I am trying to determine if there is a way for me to write an if statement that will result in true if three values are equal to each other.
EX:
if a == b == c
puts "true"
end
Instead of having to write:
if a == b && a == c && b == c
I am trying to determine if there is a way for me to write an if statement that will result in true if three values are equal to each other.
EX:
if a == b == c
puts "true"
end
Instead of having to write:
if a == b && a == c && b == c
You don't need all 3, if a equal b and c equal b it implies a equal c.
If you need it more scalable 3+ arguments, you can do:
Set[a, b, c].size == 1
Original answer was:
Set[a, b, c].one?
But it one? does not count nil or false elements. So Set[nil, nil].one? will be false.
Thanks to Sagar Pandya for pointing it out in the comments.
As @Bodacious mentioned a == b && b == c would work due to the transitive property.
But if you have many items, you could do something like:
values = [a, b, c]
if values.all? { |value| value == value[0] }
puts "true"
end
The all? method returns true if a block returns true for every item. So in that block, check to see if every item is equal to the first item.