By default, the ||
and &&
operators use the Short-circuit evaluation
principle
irb> true || puts('short-circuit evaluation')
=> true
irb> false && puts('short-circuit evaluation')
=> false
For both statements the left side of the expression suffices to determine the value of the expression.
So, the puts('short-circuit evaluation')
is never evaluated.
Sometimes, you need to evaluate the whole expression.
To do so, you can use the |
and &
operators.
They are both methods of the TrueClass
and FalseClass
classes
irb> false & puts('avoid short-circuit evaluation')
avoid short-circuit evaluation
=> false
irb> true | puts('avoid short-circuit evaluation')
avoid short-circuit evaluation
=> true