0
我该如何做这样的事情?还是我需要一直使用IF?WHEN有可能检查变量是否属于数组?
ar = [["a","b"],["c"],["d","e"]]
x = "b"
case x
when ar[0].include?(x)
puts "do something"
when ar[1].include?(x)
puts "do else"
when ar[2].include?(x)
puts "do a 3rd thing"
end
我使用Ruby 1.8.7
'each_with_index'将适合在这里:'I = {ar.each_with_index | E,I |如果e.include?(x)}' – 2010-01-21 16:08:58
@glenn,'each_with_index'返回整个数组。 'p [1,2] .each_with_index {| e,i |我如果e == 2}# - > [1,2]' – vava 2010-01-22 09:45:38
没错。 '我= ar.each_with_index.find {| e,i | e.include?(x)} [1]'......对你的回答没有太大的改进。 – 2010-01-22 17:50:31