Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Difference between nil?, empty?, blank? and present? in Ruby and Ruby on Rasils.
# nil? can be used on any Ruby object. It returns true only if the object is nil.
nil.nil? # => true
[].nil? # => false
{}.nil? # => false
"".nil? # => false
" ".nil? # => false
true.nil? # => false
# empty? can be used on some Ruby objects including Arrays, Hashes and Strings. It returns true only if the object's length is zero.
nil.empty? # NoMethodError: undefined method `empty?' for nil:NilClass
[].empty? # => true
{}.empty? # => true
"".empty? # => true
" ".empty? # => true
true.empty? # => NoMethodError: undefined method `empty?' for true:TrueClass
# blank? is an ActiveSupport extension to Ruby Object and returns true for nil, false, empty, or a white-space string.
[ "", " ", false, nil, [], {} ].all?(&:blank?) # => true
# present? is also an ActiveSupport extension to Ruby Object and it is the negation of blank?
[ "", " ", false, nil, [], {} ].any?(&:present?) # => false
@ArthurZ

This comment has been minimized.

Copy link

@ArthurZ ArthurZ commented Jan 25, 2018

How come in your case on ln 14 is gives a true, and in mine it is a false?

irb(main):06:0> " ".empty? 
=> false
@panoysia

This comment has been minimized.

Copy link

@panoysia panoysia commented Feb 18, 2018

Line 14 needs to be corrected as "" is different from " " when checking/calling for #empty?.

The former is an empty string therefore returns True, while the latter is a white/blank-spaced string and therefore returns False.

@CyberMew

This comment has been minimized.

Copy link

@CyberMew CyberMew commented May 15, 2018

Line 14 is correct. Pure whitespaces is still counted as blank/empty. https://stackoverflow.com/a/888877/321629

@gr8bit

This comment has been minimized.

Copy link

@gr8bit gr8bit commented Oct 27, 2018

Line 14 is wrong. Only string length of 0 is empty. The Stackoverflow link says exactly that.

" ".empty?   # => false
@prateekshrivastav786

This comment has been minimized.

Copy link

@prateekshrivastav786 prateekshrivastav786 commented Apr 30, 2019

Since empty checks the length of the object on which it is applied, so, line 14 needs to be corrected, It should be false. On the same node, line 13 has string length 0 hence it will be true.
irb(main):001:0> "".empty? => true irb(main):002:0> " ".empty? => false irb(main):003:0>
Cheers !!!

@prateekshrivastav786

This comment has been minimized.

Copy link

@prateekshrivastav786 prateekshrivastav786 commented Apr 30, 2019

Since "empty" checks the length of the object and in the case of line 14, it does has hence it will be false. Subsequently, line 13 has no space hence no length of any string so, it will be false. As can be checked below:
irb(main):001:0> "".empty? => true irb(main):002:0> " ".empty? => false irb(main):003:0>

Cheers!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.