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

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

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

How to use a splat as a ruby method parameter [duplicate]

This question already has an answer here:
naked asterisk as parameter in method definition: def f(*)
(1 answer)
Closed 6 years ago.
I'm looking at a ruby method
def test(*)
puts "hello"
end
I'm confused about the *. Obviously if I run test it returns "hello". But what if I pass an argument into test...
test("this argument")
How do I call that method within the test method yet still have the splatter? I'm just awfully confused about having a splatter without a name. How does it work?
This post has a fairly detailed low level explanation: http://blog.honeybadger.io/ruby-splat-array-manipulation-destructuring/
To quote the most relevant part:
def go(x, *args, y)
puts x # => 1
puts y # => 5
puts args.inspect # => [2,3,4]
end
go(1, 2, 3, 4, 5)

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.

Meaning of ||= symbol [duplicate]

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.

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