It occurred to me after reviewing all the feedback to my last post that despite making the connection to statement modifiers, I never directly contrasted the two. And that this might be an even simpler way of explaining the usefulness of
or in Ruby. I’ve added the following examples to the original post, but in case you missed it:
You can think of
and is as a reversed
if statement modifier:
next if widget = widgets.pop
widget = widgets.pop and next
Because of the low precedence of
and, no extra parentheses are needed here as they would be for
or is a reversed
unless statement modifier:
raise "Not ready!" unless ready_to_rock?
ready_to_rock? or raise "Not ready!"
Which version to use – if/unless or and/or – depends on your taste and what reads well in a given case. But it’s nice to have the option.