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
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 ||= do in Ruby 1.9.2?
It assigns []
to params["user][:role_ids]
if params["user][:role_ids]
is nil
or another falsy value...
Otherwise, it retains the original value of params["user][:role_ids]
Example
variable = nil
variable ||= "string"
puts variable # "string"
variable2 = "value"
variable2 ||= "string"
puts variable2 # "value"
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.
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 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 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.
||=' 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.
How can I disable a hotkey in GreaseMonkey while editing?
document.activeElement works for me in FF3 but the following also works
(function() {
var myActiveElement;
document.onkeypress = function(event) {
if ((myActiveElement || document.activeElement || {}).tagName != 'INPUT')
// do your magic
};
if (!document.activeElement) {
var elements = document.getElementsByTagName('input');
for(var i=0; i<elements.length; i++) {
elements[i].addEventListener('focus',function() {
myActiveElement = this;
},false);
elements[i].addEventListener('blur',function() {
myActiveElement = null;
},false);
}
}
})();
Related Topics
How to Use Global Variables or Constant Values in Ruby
List of Ruby Operators That Can Be Overridden/Implemented
How to Get Ruby/Homebrew/Rvm to Work on Yosemite
Ruby Run Shell Command in a Specific Directory
Can't Install Rails on MAC Os Catalina
Find the Newest Record in Rails 3
How to Dynamically Call a Math Operator in Ruby
Getting Fields_For and Accepts_Nested_Attributes_For to Work With a Belongs_To Relationship
How to Write Postgresql Functions on Ruby on Rails
Confusion With Atomic Grouping - How It Differs from the Grouping in Regular Expression of Ruby
Checking If a Variable Is Defined in Sass
How to Redirect Stderr and Stdout to File For a Ruby Script
Google Plus API Shutdown Today, Which Alternative Can Be Used to Authentication