Reputation: 49
My code is returning the 1st statement in control-flow
a = 6
b = 13
c = 4
if a == 2||4||5 && b == 9||10||11
puts "staement1"
elsif a == 6||7||8 && b == 12||13||14
puts "statement2"
elsif puts c
end
output is "statement1" but it should be "statement2". what is the issue ?
Upvotes: 1
Views: 66
Reputation: 10107
Lets talk about a == 2 || 4 || 5
.
It isn't equivalent to a == 2 || a == 4 || a == 5
, but is evaluated in such order:
a == 2
is false
false || 4
is 44 || 5
is not evaluated and short circuited.Therefore, a == 2 || 4 || 5
's value is 4...
The same rule applies for b == 9||10||11
... etc.
Upvotes: 2
Reputation: 30071
Your if is like
a = 6
b = 13
if (a == 2)||4||5 && (b == 9)||10||11
so in the end
4 && 10
and this is true
because the only falsey values in ruby are nil
and false
itself
maybe what you want is something like
if [2, 4, 5].include?(a) && [9, 10, 11].include?(b)
Upvotes: 4