How to Understand Nil Vs. Empty Vs. Blank in Ruby

How to understand nil vs. empty vs. blank in Ruby

.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

Difference between .nil?, .blank? and .empty?

In Ruby, nil in an object (a single instance of the class NilClass). This means that methods can be called on it. nil? is a standard method in Ruby that can be called on all objects and returns true for the nil object and false for anything else.

empty? is a standard Ruby method on some objects like Arrays, Hashes and Strings. Its exact behaviour will depend on the specific object, but typically it returns true if the object contains no elements.

blank? is not a standard Ruby method but is added to all objects by Rails and returns true for nil, false, empty, or a whitespace string.

Because empty? is not defined for all objects you would get a NoMethodError if you called empty? on nil so to avoid having to write things like if x.nil? || x.empty? Rails adds the blank? method.


After answering, I found an earlier question, "How to understand nil vs. empty vs. blank in Rails (and Ruby)", so you should check the answers to that too.

When to use nil, blank, empty?

  • nil? - checks to see if variable is referencing an object or not

  • empty? - may be used to check on various object types like empty string "" or empty array []

  • blank? - checks for nil? or empty?.

nil v/s empty in ruby

Apparently @stores is not nil but initialized to an empty array [].

So !@stores.nil? is true because @stores.nil? is false. I'm not familiar with logger.info so I'm not sure why it prints nil for the third line. empty? returns true if there is no element in the array.

Difference between empty? and nil? methods

I'm guessing you are coming from python? Python and other languages can be quite loose about tests for various design reasons.

Ruby takes the strict approach.

nil? is only for testing nil (no value).

empty? is testing for containers (arrays, hashes) that have nothing in them.

Search for ruby documentation on those methods. For example, here is empty for array.

You might also like this tutorial.

Testing for empty or nil-value string

The second clause does not need a !variable.nil? check—if evaluation reaches that point, variable.nil is guaranteed to be false (because of short-circuiting).

This should be sufficient:

variable = id if variable.nil? || variable.empty?

If you're working with Ruby on Rails, Object.blank? solves this exact problem:

An object is blank if it’s false, empty, or a whitespace string. For example, "", " ", nil, [], and {} are all blank.

The Array contain nil element or not?

array.nil? checks if array is nil, not if it contains a nil value. Similarly, array.empty? checks if array contains no elements.

If you want to check if any element is nil, use any?.

array.any? { |element| element.nil? }

That can be shortened to array.any?(nil).

Rails ||= for empty strings

you can try like this:

@my_var = ''
@my_var = @my_var.presence || 'This is a non-empty string'

Thanks :-)



Related Topics



Leave a reply



Submit