Reputation: 6612
Basically, I am trying to check if 6 of my values are the same. I tried stringing them:
if val1 == val2 == val3 == val4 == val5 == val6
#...
end
But this errors out. Is this possible using another method? Thanks
Upvotes: 6
Views: 2358
Reputation: 8120
If values are by any chance Fixnum, this sexy line would work:
if val1 == val2 & val3 & val4 & val5 & val6
# ...
end
if not, then this fatty would work for any type
if [val1] == [val2] & [val3] & [val4] & [val5] & [val6]
# ...
end
Upvotes: 2
Reputation: 22258
Try this:
if [val1, val2, val3, val4, val5, val6].uniq.count == 1
#...
end
If you wanna get fancy, you can try this
unless [val2, val3, val4, val5, val6].find{ |x| x != val1 }
# ...
end
The above will stop as soon as it finds an element that is not equal to val1
, otherwise, the block will be executed.
Upvotes: 11
Reputation: 20408
A cute way:
[val1,val2,val3,valN].uniq.size == 1
A more prosaic way:
[val2,val3,valN].all?{ |x| x == val1 }
Upvotes: 2