Ruby: Bar bar equal
A common sight in Ruby code is ||=
:
What’s that you say?
||=
is used for conditional assignment:
One caveat is that the implementation of ||=
is conditional on whether a is eithernil
or false
, so
If it helps to think of it this way, the closest easy approximation would be a || a = 24
.
Another far less used, but pretty cool one is &&=
, as you can imagine, its the reverse. It will assign only if there is a non-nil
, non-false
value present: