How do I express multiple or options in a conditional statement in Ruby? I thought something like this would work but it doesn’t:
1 == (3 || 2 || 1)
(1 == (3 || 2 || 1))
I thought those would return true.
I want a way to say if any number of a group of things are true then return true. Do I have to spell it out the long way?
if (1 == 3 || 1 == 2 || 1 == 1)
In English I would say it
If 1 equals 3, 2, or 1, then return true.
Yes, that’s one way to do it. More Rubyesque would be:
The reason that
1 == (3 || 2 || 1)doesn’t work is because(3 || 2 || 1)is evaluated first. The||operator returns the first value if it’s truthy, and the second if the first is falsy.Thus
(3 || 2 || 1)is3, so you’re comparing1 == 3, which is obviouslyfalse.