What does ||= (or-equals) mean in Ruby?
This question has been discussed so often on the Ruby mailing-lists and Ruby blogs that there are now even threads on the Ruby mailing-list whose only purpose is to collect links to all the other threads on the Ruby mailing-list that discuss this issue.
Here's one: The definitive list of ||= (OR Equal) threads and pages
If you really want to know what is going on, take a look at Section 11.4.2.3 "Abbreviated assignments" of the Ruby Language Draft Specification.
As a first approximation,
a ||= b
is equivalent to
a || a = b
and not equivalent to
a = a || b
However, that is only a first approximation, especially if a
is undefined. The semantics also differ depending on whether it is a simple variable assignment, a method assignment or an indexing assignment:
a ||= b
a.c ||= b
a[c] ||= b
are all treated differently.
What does ||= mean?
Basically, a ||= b
means assign b to a if a is null or undefined or false (i.e. false-ish value in ruby)
, it is similar to a = b unless a
, except it will always evaluate to the final value of a
(whereas a = b unless a
would result in nil
if a
was true-ish).
Ruby's ||= (or equals) in JavaScript?
Both are absolutely correct, but if you are looking for something that works like ||=
in ruby. The first method which is variable = variable || {}
is the one you are looking for :)
What does ||= mean?
It is an assignment operator which means: or assign this value to a variable.
So if you did something like x ||= y
this meansx || x = y
so if x is nil or false set x to be the value of y.
doubts regarding ||= OR EQUALS operator in ruby
In Ruby, there are two values that are considered logical false. The first is the boolean value false
, the other is nil
. Anything which is non-nil
and not explicitly false
is true
. The first time though the method, @num
is nil
, which is treated as false
and the logical or
portion of ||=
needs to be evaluated and ends up assigning the empty array to @num
. Since that's now non-nil
, it equates to true
. Since true || x
is true no matter what x
is, in future invocations Ruby short circuits the evaluation and doesn't do the assignment.
Ruby |= assignment operator
Bitwise OR assignment.
x |= y
is shorthand for:
x = x | y
(just like x += y
is shorthand for x = x + y
).
'||=' operator in Ruby
It's an assignment operator for 'Conditional Assignment'
See here -> http://en.wikibooks.org/wiki/Ruby_Programming/Syntax/Operators
Conditional assignment:
x = find_something() #=>nil
x ||= "default" #=>"default" : value of x will be replaced with "default", but only if x is nil or false
x ||= "other" #=>"default" : value of x is not replaced if it already is other than nil or false
Operator ||= is a shorthand form of the expression:
x = x || "default"
EDIT:
After seeing OP's edit, the example is just an extension of this, meaning:
car = method_1 || method_2 || method_3 || method_4
Will assign the first non-nil or non-false return value of method_1, method_2, method_3, method_4 (in that order) to car
or it'll retain its old value.
What does ||= do in Ruby
This will assign bar
to foo
if (and only if) foo
is nil
or false
.
EDIT: or false, thanks @mopoke.
Double Pipe Symbols in Ruby Variable Assignment?
It's a conditional assignment. From here:
x = find_something() #=>nil
x ||= "default" #=>"default" : value of x will be replaced with "default", but only if x is nil or false
x ||= "other" #=>"default" : value of x is not replaced if it already is other than nil or false
Related Topics
Why Is "Slurping" a File Not a Good Practice
How to Pick Randomly from an Array
Redirect_To Using Post in Rails
What Are the Ruby Gotchas a Newbie Should Be Warned About
Ruby: Inherit Code That Works With Class Variables
What's the Best Way to Use Soap With Ruby
How to Work With Two Different Databases in Rails With Active Records
Combining Implicit Wait and Explicit Wait Together Results in Unexpected Wait Times
Begin, Rescue and Ensure in Ruby
How to Install Postgresql'S Pg Gem on Ubuntu
Require': Cannot Load Such File - 'Nokogiri\Nokogiri' (Loaderror) When Running 'Rails Server'