x ||= y
means assigning y
to x
if x is null or undefined or false ; it is a shortcut to x = y unless x
.
With Ruby short-circuit operator ||
the right operand is not evaluated if the left operand is truthy.
Now some quick examples on my above lines on ||=
:
when x is undefined and n is nil
:
with unless
y = 2
x = y unless x
x # => 2
n = nil
m = 2
n = m unless n
m # => 2
with =||
y = 2
x ||= y
x # => 2
n = nil
m = 2
n ||= m
m # => 2