The problem here is priority. orhas a lower priority than ||. So your first statement evaluates
(x = nil) or 4
The result of the expression 4(so you thought that it works correctly in irb), but xis assigned nilbecause it orhas a lower priority than =.
The version ||does what you want:
x = (nil || 4)
Ed S. source
share