Ruby is awesome but sometimes it throws me a curve ball.
x = 'bar' case x when 'foo' || 'bar' puts x end
At first glance I expected this to output ‘bar’, but instead nothing!
Looking at the if statement equivalent, it becomes clear.
x = 'bar' if x == ('foo' || 'bar') puts x end
The “||” operator is evaluated before the comparison is made. Here’s the correct case statement format to get a ‘bar’:
x = 'bar' case x when 'foo', 'bar' puts x end # => bar