Explore more about .nil? .empty? .blank? .present? in Ruby on Rails

nil?, empty? and blank? and .present? are little bit confusing.

We as a Developer needs to be careful in using them and it is better that we digest the details before using it.

.nil?

  • It is Ruby method
  • It can be used on any object and is true if the object is nil.
  • “Only the object nil responds true to nil?” – RailsAPI

In Ruby, all classes inherit from the Object class. nil? is a method of Object; therefore, unless explicitly overridden, all classes have access to nil?. To better understand how Ruby expresses the concept of a non existent object, we need to look at its pseudo-variable nil. The same idea in other languages is often mapped to the primitive type, null or NULL. Depending on the language, NULL may point to nothing at all. Ruby differs here, in that, nil references an actual class called NilClass which exposes methods. This is why it’s possible to do seemingly illogical things like:

nil.nil?    => true
When calling on an object that is non existent, invalid or explicitly set to nil, an instance of the singleton NilClass is returned.

A few examples will illustrate how this works.

posts= {:name=> “my post”, :description=> “my description”}

posts[:name]   => nil
posts[:name].class  => NilClass
posts[:name].nil? => true
posts[:description].nil? => false

my_test_var = nil
my_test_var .nil? => true

[].nil? => false

“”.nil? => false

0.nil? => false

false.nil? => false

empty?

Unlike nil?, empty? is only available on some Ruby objects. String, Hash and Array are a few of the classes that implement it. When calling empty? the receiving object is being checked to see if it possesses a non-nil value or values. empty? (at least for the mentioned classes) returns either true or false.

  • It is Ruby method
  • 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

[“name”, “title”, “description”].empty? => false

[“”].empty? => false

[].empty? => true

{}.empty?=> true

“”.empty?=> true

0.empty? => NoMethodError: undefined method `empty?’ for 0:Fixnum

my_test_var2 = nil
my_test_var2 .empty? = > NoMethodError: undefined method `empty?’ for nil:NilClass
The last example above clearly illustrates why one would get unexpected results when trying to do things like this:

dog = {:name => “Beauregard”}
puts “What kind?” if dog[:breed].empty?
=> NoMethodError: undefined method `empty?’ for nil:NilClass

# Use nil? instead.
puts “What kind?” if dog[:breed].nil?
=> What kind?

.blank?

  • It is Rails method
  • operate on any object as well as work like .empty? on strings, arrays and hashes.
  • It also evaluates true on strings which are non-empty but contain only whitespace:

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

For instance, I often use blank? when operating on form field values that are considered optional by a model. Going back to the dog hash, we can see how blank? simplifies testing for values.

nil.blank? = true
[].blank? = true
{}.blank? = true
“”.blank? = true
5.blank? == false
dog = {:name => “Beauregard”}
puts “What kind?” if dog[:breed].blank?
=> What kind?

dog = {:name => “Beauregard”, :breed => “”}
puts “What kind?” if dog[:breed].blank?
=> What kind?
” “.blank? == true” “.empty? == false

present?


Quick tip: !obj.blank? == obj.present?
activesupport/lib/active_support/core_ext/object/blank.rb, line 17 # (Ruby 1.9)

def present?
!blank?
end

 

Advertisements

One thought on “Explore more about .nil? .empty? .blank? .present? in Ruby on Rails

  1. First of all, Thanks for these great articles/posts. I have some issue stated as below:

    Under Section Empty?
    You stated as line “operate on any object as well as work like .empty? on strings, arrays and hashes”

    does it mean that it operates like nil? i.e we can apply it on objects of any class. or
    it apply to array, hashes and string objects only.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s