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