Comment puis-je faire quelque chose comme ça? ou dois-je utiliser IF tout le temps?Est-il possible pour WHEN de vérifier si la variable appartient à un tableau?
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
J'utilise Ruby 1.8.7
'each_with_index' cadrerait ici:' i = {ar.each_with_index | e, i | Si e.include? (x)} ' –
@ glenn,' each_with_index' renvoie un tableau entier. 'p [1, 2] .each_with_index {| e, je | i si e == 2} # -> [1, 2] ' – vava
à droite. 'i = ar.each_with_index.find {| e, je | e.include? (x)} [1] '... pas une grande amélioration par rapport à votre réponse. –