Skip to content

Instantly share code, notes, and snippets.

@sandheepg
Created March 8, 2017 07:54
Show Gist options
  • Save sandheepg/b134e247f2aa56a8eea4524815a58038 to your computer and use it in GitHub Desktop.
Save sandheepg/b134e247f2aa56a8eea4524815a58038 to your computer and use it in GitHub Desktop.
||= operator in ruby
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment