What does the ||= operand stand for in ruby
It is the shorthand for a logical OR operation. It is equivalent to:
a || a = b
Note: The above code sample has been corrected to reflect the true (if unintuitive) behavior if expanding a ||= b
. Thanks to the people who pointed that out for me. Here is the source
if a
evaluates to true it will remain as is, otherwise b
will be assigned to a
. In ruby nil
evaluates to false
, so you can see how this is useful for lazy loading and default value assignment.
What does the '||=' operator do in ruby?
what does || do? If you have a and b then a || b
is true if and only if either a or b is true. It is the same with ||= this operator combines two operations '=' and '||'. So a ||= b
is equivelent to c || c = b
EDIT: so in your context ENV['ENVIRONMENT'] ||= 'test' means that if ENV['ENVIRONMENT'] is not nil and not false it will preserve its value, otherwise it will become 'test' and after that the new value of ENV['ENVIRONMENT'] is assigned to RACK_ENV
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).
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?
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.
Difference between || and ||=?
||=
will set the left-hand value to the right hand value only if the left-hand value is falsey.
In this case, both 6 and 4 are truthy, so a = 6 || 4
will set a
to the first truthy value, which is 6
.
a ||= 6
will set a
to 6 only if a
is falsey. That is, if it's nil or false.
a = nil
a ||= 6
a ||= 4
a # => 6
What does ||= mean in Ruby?
It's mainly used as a shortform for initializing a variable to a certain value, if it is not yet set.
Think about the statement as returning x || (x = y)
. If x
has a value (other than false
), only the left side of the ||
will be evalutated (since ||
short-circuts), and x
will be not be reassigned. However, if x
is false
or nil
, the right side will be evaluated, which will set x
to y
, and y
will be returned (the result of an assignment statement is the right-hand side).
See http://dablog.rubypal.com/2008/3/25/a-short-circuit-edge-case for more discussion.
Related Topics
How to Get Ruby/Homebrew/Rvm to Work on Yosemite
Include Jekyll/Liquid Template Data in a Yaml Variable
"Gem Install Rails" Fails With Dns Error
How to Solve This Trouble to Deploy a Rails App to Heroku
Fork Child Process with Timeout and Capture Output
The Class/Object Paradox Confusion
Looping in a Spiral Outside-In
Is the Server Running on Host "Localhost" (::1) and Accepting Tcp/Ip Connections on Port 5432
How to Implement a Friendship Model in Rails 3 for a Social Networking Application
Learning Insertion Sort in Ruby
Confusion With Atomic Grouping - How It Differs from the Grouping in Regular Expression of Ruby
Rails Each Loop Insert Tag Every 6 Items
Json Encoding Wrongly Escaped (Rails 3, Ruby 1.9.2)
Can't Install Ruby on Rails with Rvm on Ubuntu 13.04
How to Parse a CSV File, Update a Field, Then Save
Test If Variable Matches Any of Several Strings W/O Long If-Elsif Chain, or Case-When