Home A concise explanation of nil v. empty v. blank in Ruby on Rails
Reply: 14

A concise explanation of nil v. empty v. blank in Ruby on Rails

Arrel
1#
Arrel Published in 2009-05-19 22:51:36Z

I find myself repeatedly looking for a clear definition of the differences of nil?, blank?, and empty? in Ruby on Rails. Here's the closest I've come:

  • blank? objects are false, empty, or a whitespace string. For example, "", " ", nil, [], and {} are blank.

  • nil? objects are instances of NilClass.

  • empty? objects are class-specific, and the definition varies from class to class. A string is empty if it has no characters, and an array is empty if it contains no items.

Is there anything missing, or a tighter comparison that can be made?

alhimik45
2#
alhimik45 Reply to 2015-10-06 11:39:24Z

.nil? can be used on any object and is true if the object is nil.

.empty? can be used on strings, arrays and hashes and returns true if:

  • String length == 0
  • Array length == 0
  • Hash length == 0

Running .empty? on something that is nil will throw a NoMethodError.

That is where .blank? comes in. It is implemented by Rails and will operate on any object as well as work like .empty? on strings, arrays and hashes.

nil.blank? == true
false.blank? == true
[].blank? == true
{}.blank? == true
"".blank? == true
5.blank? == false
0.blank? == false

.blank? also evaluates true on strings which are non-empty but contain only whitespace:

"  ".blank? == true
"  ".empty? == false

Rails also provides .present?, which returns the negation of .blank?.

Array gotcha: blank? will return false even if all elements of an array are blank. To determine blankness in this case, use all? with blank?, for example:

[ nil, '' ].blank? == false
[ nil, '' ].all? &:blank? == true 
the Tin Man
3#
the Tin Man Reply to 2012-11-14 14:53:41Z

One difference is that .nil? and .empty? are methods that are provided by the programming language Ruby, whereas .blank? is something added by the web development framework Rails.

davemyron
4#
davemyron Reply to 2009-05-19 23:51:50Z

Don't forget any? which is generally !empty?. In Rails I typically check for the presence of something at the end of a statement with if something or unless something then use blank? where needed since it seems to work everywhere.

Alexander Malfait
5#
Alexander Malfait Reply to 2010-07-13 11:33:23Z

Quick tip: !obj.blank? == obj.present?

Can be handy/easier on the eyes in some expressions

Alan H
6#
Alan H Reply to 2012-11-14 13:56:02Z

A special case is when trying to assess if a boolean value is nil:

false.present? == false
false.blank? == true
false.nil? == false

In this case the recommendation would be to use .nil?

the Tin Man
7#
the Tin Man Reply to 2016-01-29 18:27:40Z

I made this useful table with all the cases:

Sibevin Wang
8#
Sibevin Wang Reply to 2016-09-04 14:09:37Z

Just extend Julian's table:

Ref: empty?blank?nil?傻傻分不清楚

the Tin Man
9#
the Tin Man Reply to 2016-01-29 18:27:21Z

nil? is a standard Ruby method that can be called on all objects and returns true if the object is nil:

b = nil
b.nil? # => true

empty? is a standard Ruby method that can be called on some objects such as Strings, Arrays and Hashes and returns true if these objects contain no element:

a = []
a.empty? # => true

b = ["2","4"]
b.empty? # => false

empty? cannot be called on nil objects.

blank? is a Rails method that can be called on nil objects as well as empty objects.

the Tin Man
10#
the Tin Man Reply to 2016-01-29 18:25:33Z

Everybody else has explained well what is the difference.

I would like to add in Ruby On Rails, it is better to use obj.blank? or obj.present? instead of obj.nil? or obj.empty?.

obj.blank? handles all types nil, '', [], {}, and returns true if values are not available and returns false if values are available on any type of object.

mahatmanich
11#
mahatmanich Reply to 2015-08-19 13:52:27Z

Rails 4

an alternative to @corban-brook 's 'Array gotcha: blank?' for checking if an arrays only holds empty values and can be regarded as blank? true:

[ nil, '' ].all? &:blank? == true

one could also do:

[nil, '', "", " ",'  '].reject(&:blank?).blank? == true
the Tin Man
12#
the Tin Man Reply to 2016-01-29 18:23:46Z

nil? can be used on any object. It determines if the object has any value or not, including 'blank' values.

For example:

example = nil
example.nil?  # true

"".nil?  # false

Basically nil? will only ever return true if the object is in fact equal to 'nil'.

empty? is only called on objects that are considered a collection. This includes things like strings (a collection of characters), hashes (a collection of key/value pairs) and arrays (a collection of arbitrary objects). empty? returns true is there are no items in the collection.

For example:

"".empty? # true
"hi".empty?   # false
{}.empty?  # true
{"" => ""}.empty?   # false
[].empty?   # true
[nil].empty?  # false

nil.empty?  # NoMethodError: undefined method `empty?' for nil:NilClass

Notice that empty? can't be called on nil objects as nil objects are not a collection and it will raise an exception.

Also notice that even if the items in a collection are blank, it does not mean a collection is empty.

blank? is basically a combination of nil? and empty? It's useful for checking objects that you assume are collections, but could also be nil.

ndn
13#
ndn Reply to 2016-02-18 19:05:37Z

exists? method can be used to check whether the data exists in the database or not. It returns boolean values either true or false.

steve
14#
steve Reply to 2018-01-15 18:17:51Z

  • Everything that is nil? is blank?
  • Everything that is empty? is blank?
  • Nothing that is empty? is nil?
  • Nothing that is nil? is empty?

tl;dr -- only use blank? & present? unless you want to distinguish between "" and " "

Mr_pzling_Pie
15#
Mr_pzling_Pie Reply to 2018-02-03 15:35:41Z

nil? nil can be used on any object. It determines if the object has any value or not, including 'blank' values.

For example:

example = nil
example.nil?  # true

"".nil? # false Basically .nil? will only ever return true if the object is in fact equal to 'nil'

empty? empty is only called on objects that are considered a collection. This includes things like strings (a collection of characters), hashes (a collection of key/value pairs) and arrays (a collection of arbitrary objects). .empty? returns true is there are no items in the collection.

For example:

"".empty? # true
"hi".empty?   # false
{}.empty?  # true
{"" => ""}.empty?   # false
[].empty?   # true
[nil].empty?  # false

nil.empty? # NoMethodError: undefined method `empty?' for nil:NilClass Notice that .empty? can't be called on nil objects as nil objects are not a collection and it will raise an exception.

Also notice that even if the items in a collection are blank, it does not mean a collection is empty.

blank? blank is basically a combination of .nil? and .empty? It's useful for checking objects that you assume are collections, but could also be nil

For example:

"".blank? # true
"hi".blank?   # false
{}.blank?  # true
{"" => ""}.blank?   # false
[].blank?   # true
[nil].blank?  # false

nil.blank? # true

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.352826 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO