This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does ||= (or equals) mean in Ruby?
What does ||= mean?
I have just started learning RubyMotion and in a lot of examples I see the ||= syntax. What does this mean?
Here is an example:
def window
#window ||= begin
w = UIWindow.alloc.initWithFrame UIScreen.mainScreen.bounds
w.rootViewController = #navigationController
w
end
It is difficult to search symbols, google ignored the symbols in my query.
It is an assignment operator which means: or assign this value to a variable.
So if you did something like x ||= ythis meansx || x = y so if x is nil or false set x to be the value of y.
This Operator only sets the variable if the variable is false or Nil.
Related
This question already has answers here:
What does ||= (or-equals) mean in Ruby?
(23 answers)
Closed 6 years ago.
While programming routes in Sinatra, I came across code listed as this:
before do
session[:lists] ||= []
end
What is this operation doing ||= []?
x ||= value is a way to say "if x contains a falsey value, including
nil, assign value to x"
That's setting session[:lists] equal to [] if session[:lists] is falsey.
Related to https://stackoverflow.com/a/6671466/4722305.
It sets [] to session[:lists] when it's nil or falsy
Read more here
;)
This question already has answers here:
What does ||= (or-equals) mean in Ruby?
(23 answers)
Closed 7 years ago.
I recently saw a code which looks something like this
#a ||=
if x
x/2
else
2 * x
What is the use of ||=
It means execute the assignment if the variable is falsey.
So if #a is falsey (e.g. nil or false) the code afterwards is run and it's return value assigned to #a
This works because a OR statement is true, if the first operand is true and therefore does not need be further executed.
It's equivalent to the longer expression:
unless #a
if x
#a = x/2
else
#a = 2*x # though this line is kind of weird if x is falsey^^
end
end
This question already has answers here:
What does ||= (or-equals) mean in Ruby?
(23 answers)
Closed 7 years ago.
I'm still pretty green when it comes to Ruby and am trying to figure out what this is doing:
command_windows.each {|window| window.hidden ||= window.open? }
The command_windows variable appears to be an array of objects. If someone could explain to me what this line of code means, particularly what the ||= symbol is I would appreciate it.
foo ||= "bar" is the equivalent of doing foo || foo = "bar".
As Mischa explained, it checks for a falsy value before assigning.
In your case, you could think of it as:
command_windows.each {|window| window.hidden || window.hidden = window.open? }
which is another way of saying
command_windows.each {|window| window.hidden = window.open? unless window.hidden }
The ||= operator is used to assign new value to variable. If something was assigned to it before it won't work. It is usually used in hashes, so you don't have to check, if something is already assigned.
This question already has answers here:
'||=' operator in Ruby
(7 answers)
Closed 9 years ago.
I have a simple question about the meaning of a symbol (I think). What's the mean of ||= in ruby? I have a code snippet that say:
... ||= [nil]
Is as "<<" ? ordinary method?
x ||= y
means (almost) the same thing as
x = x || y
(it only evaluates x once, though.)
It is used mostly for checking if a variable is falsy (nil or false), and if so, setting it to a default value.
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
Why is `a = a` `nil` in Ruby?
I'm sure there is a reason for this behavior i'm just curious what it is.
y = x # NameError: undefined local variable or method 'x'
x = x # => nil
This is caused by the way variables are initialized in Ruby, which is rather unique to this language. Basically, Ruby initializes (creates) a variable if it could possibly get assigned a value. Consider this example:
if false
x = "hello"
end
x will definitely not get assigned the "hello" string here. However, it will still get initialized with nil as from the static program analysis, it could have been assigned.
Your example is similar. Because you assign something to x, it will get initialized with nil before the statement is executed. Thus, during execution, x is in fact nil.