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 and / or in Ruby. I’ve added the following examples to the original post, but in case you missed it:

and

You can think of and is as a reversed if statement modifier:

next if widget = widgets.pop

becomes:

next if widget = widgets.pop

Because of the low precedence of and , no extra parentheses are needed here as they would be for && .

or

Likewise, or is a reversed unless statement modifier:

raise "Not ready!" unless ready_to_rock?

becomes:

ready_to_rock? or raise "Not ready!"

Conclusion

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.

[ad#PostInline]