Meaning of ||= symbol [duplicate] - ruby

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.

Related

What is this in Ruby? ||= [duplicate]

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
;)

What is the function of ||= operator/s in ruby? [duplicate]

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

What does "||=" mean in Ruby? [duplicate]

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.

What does the !! mean in this Ruby function? [duplicate]

This question already has answers here:
What does !! mean in ruby?
(8 answers)
Closed 9 years ago.
What does the !! mean in this Ruby function?
def is_i?
!!(self =~ /^[-+]?[0-9]+$/)
end
It makes sure the response is a boolean. So nil or false wil become false, any other value becomes true

What does ||= mean? [duplicate]

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.

Resources