||= operator in Ruby

Posted . Visible to the public.
x ||= y is similar to x || x = y.

In the above statement, if x evaluates to logical true then assignment is not done.
If x evaluates to logical false only then the assignment is done.

Though the above two statements are similar, there are subtle differences.

# if x is not defined
puts x || x=y  ## Undefined local variable or method `x`
puts x ||= y   ## prints y
Sandheep
Posted by Sandheep to Sandheep's deck (2013-04-25 14:35)