I would like to print one of two strings dependently on the condition value in Ruby.
Of course it can be always done in the most classic way:
if a==1 then puts "Yes!" else puts "No!" end
or even
puts (a==1 ? "Yes!" : "No!")
but I'm looking for more Ruby/Python way using lists/arrays. In Python it can be done with:
print ['Yes', 'No'][1==2]
Is there any similar way to achieve this with Ruby? The code above (written in Ruby) doesn't work, because of boolean value as an index and it doesn't work even if I'd try (1==2).to_i...
Any ideas?
puts a == 1 ? "Yes!" : "No!". And you still want it shorter? Besides the minimum things that express your information (puts,a == 1,"Yes!", and"No"), there are only two characters extra:?and:. How can it be shorter?puts ["Yes!", "No!"][[1].index(a) || 1].