difference between .nil? and == nil [duplicate] - ruby

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?.

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

Why are there slight different behaviors with array accesses? [duplicate]

This question already has answers here:
Array slicing in Ruby: explanation for illogical behaviour (taken from Rubykoans.com)
(10 answers)
Trying to understand Ruby arrays [duplicate]
(1 answer)
Closed 9 years ago.
My array is:
array = [:peanut, :butter, :and, :jelly]
array[4,0] gives []
But:
array[5,0] gives nil
I was suspecting array[4,0] should also give nil as the array has only a third index which is :jelly.
Can any Ruby punters out there clarify this behaviour?
It's all in the documentation:
Additionally, an empty array is returned when the starting index for an element range is at the end of the array.
Returns nil if the index (or starting index) are out of range.

How to check whether a string is an integer in Ruby? [duplicate]

This question already has answers here:
How to test if a string is basically an integer in quotes using Ruby
(19 answers)
Test if string is a number in Ruby on Rails
(13 answers)
Closed 9 years ago.
I have a string "1234223" and I want to check whether the value is an integer/number.
How can I do that in one line?
I have tried
1 =~ /^\d+$/
=> nil
"1a" =~ /^\d+$/
=> nil
Both line are returning nil
If you're attempting to keep similar semantics to the original post, use either of the following:
"1234223" =~ /\A\d+\z/ ? true : false
#=> true
!!("1234223" =~ /\A\d+\z/)
#=> true
A more idiomatic construction using Ruby 2.4's new Regexp#match? method to return a Boolean result will also do the same thing, while also looking a bit cleaner too. For example:
"1234223".match? /\A\d+\z/
#=> true
How about Integer("123") rescue nil ?
You can use regex
"123".match(/\A[+-]?\d+?(\.\d+)?\Z/) == nil ? false : true
It will also check for decimals.
"1234223".tap{|s| break s.empty? || s =~ /\D/}.!

Destroy a variable in ruby [duplicate]

This question already has answers here:
Undefine variable in Ruby
(5 answers)
Closed 9 years ago.
I want to destroy a variable in ruby as if it had never existed. Here is an example:
> defined? a
=> "nil"
> a = 1
> defined? a
=> "local-variable"
Now I need to set variable a to "nil" when I do defined?.
I tried some things like:
> a = nil #Not working
=> nil
> defined? a
=> "local-variable"
But nothing seems to work.
As of now (MRI 2.2 and before), there's no way to do this.

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

Resources