and and or
In a comment on Ruby Stylista, I mentioned how I use the
or keywords in Ruby. To refresh,
or work just like
||, only with very low precedence. For example:
foo = 5 && 10; foo => 10 foo = (5 && 10); foo; => 10 foo = 5 and 10; foo => 5 (foo = 5) and 10; foo => 5
You can see from the examples how the expression is 'grouped' by the very low precedence of
or. Since they have such low precedence, I use them to create conditional execution, to tie two imperative statements together. For example, you could write:
foo = fubar() if do_something()
This reverses the order of execution, putting the clause
foo = fubar() even though it will happen in the opposite order. If you wish to write them in temporal order, you can use
do_something() and foo = fubar()
This puts "first things first." Likewise you can use
or to reverse the order of an
unless statement. Instead of:
raise 'fubar' unless do_something()
You could write:
do_something() or raise 'fubar'
Again putting the first thing first. I normally only do this if both clauses are imperative. In other words, I would not rewrite either of these statements because the predicate is a query with no side-effects:
foo = fubar() if something.blank? raise 'fubar' unless something?
The predicate seems less important than the consequent, which suggests that it should come second even though the test is performed first.
NEW! Kestrels, Quirky Birds, and Hopeless Egocentricity, all of my writing about combinators, collected into one convenient and inexpensive e-book!