The ||
operator is similar to the keyword or
but is different from the keyword or
in extremely important ways. Below are two great write-ups on the topic, comparing the two and showing you how to use either one:
- http://devblog.avdi.org/2010/08/02/using-and-and-or-in-ruby/
- New version, with video: http://devblog.avdi.org/2014/08/26/how-to-use-rubys-english-andor-operators-without-going-nuts/
The most important thing to note in what Avdi says, is that ||
cannot be used for flow control, whereas or
can be.
For example…
a = :value
c = b || a
#de Since `b` is undefined/null, `c` will be set to `:value`
c = b || puts("Failure!") #de This will raise an exception!
c = b or puts("Failure!") #de Will set `c` to `NilClass` and output "Failure!"
1
solved What does || exactly Mean? [duplicate]