ruby更改数组的值

时间:2016-03-03 10:22:37

标签: ruby

我没有找到解决问题的方法。

我有这样的数组

[Thu, 01 Oct 2015 12:35:17 UTC +00:00, "10 minutes", "Successful"]
[Mon, 06 Nov 2015 12:35:17 UTC +00:00, "8 minutes", "Successful"]
[Thu, 01 Dec 2015 12:35:17 UTC +00:00, "6 minutes", "Failed"]

我想用“绿色”替换“成功”,用“红色”替换“失败”。

我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:1)

calendar.py

答案 1 :(得分:0)

如果存在两个以上不同的替换,这是另一个解决方案:

a =[ ["Thu, 01 Oct 2015 12:35:17 UTC +00:00", "10 minutes", "Successful"],
     ["Mon, 06 Nov 2015 12:35:17 UTC +00:00", "8 minutes", "Successful"],
     ["Thu, 01 Dec 2015 12:35:17 UTC +00:00", "6 minutes", "Failed"] ]

lut = {"Successful" => "green",
       "Failed"     => "red"    }

a.each do |record|
  lut.each{|k,v| record[2][k] &&= v}
end

p a

# [["Thu, 01 Oct 2015 12:35:17 UTC +00:00", "10 minutes", "green"], ["Mon, 06 Nov 2015 12:35:17 UTC +00:00", "8 minutes", "green"], ["Thu, 01 Dec 2015 12:35:17 UTC +00:00", "6 minutes", "red"]]