Ruby – Difference Between nil || false and false || nil

nullruby

nil || false returns false and false || nil returns nil. Does anyone have an explanation for this?

Best Answer

In Ruby, everything is an expression, and an expression will return the last value evaluated within it.

For both of your examples, the left side of the || expression evaluates to a falsy value, so Ruby then evaluates the right side, and returns it.

Related Question