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
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:
obj.nil? vs. obj == nil
(7 answers)
Closed 6 years ago.
I'm working through the Ruby on Rails tutorial, and I'm curious about this codeblock:
if remember_digest.nil?
false
else
BCrypt::Password.new(remember_digest).is_password?(remember_token)
end
is there a difference between if remember_digest.nil? and if remember_digest == nil, or is this just the author's preference?
No difference. According to docs, only nil object responds true to nil?.
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:
Call a method by name [duplicate]
(4 answers)
Closed 8 years ago.
I'm trying to pass min and then later max to a method, but it doesn't seem to work.
def worker(x)
[3,4].x
end
worker(min)
Do you want call methods by name? Then, use Object#send.
def worker(x)
[3,4].send(x)
end
worker(:min)
# => 3
worker(:max)
# => 4
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.