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

37,749

Solution 1

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.

Solution 2

Feel it ;)

NIL?

nil.nil?
#=> true
[].nil?
#=> false
"".nil?
#=> false
" ".nil?
#=> false

EMPTY?

[].empty?
#=> true
nil.empty?
#=> undefined method
"".empty?
#=> true
" ".empty?
#=> false

BLANK?

[].blank?
#=> true
nil.blank?
#=> true
"".blank?
#=> true
" ".blank?
#=> true

Solution 3

Any Ruby variable is an object, so it can be uninitialized/unset (set to nil). nil? method returns true if it is not initialized:

b = nil
b.nil? # true
b = 'string value'
b.nil? # false

Arrays, strings, streams in Ruby can contain no data, so they can be empty. The empty? method returns true if so:

array = []
array.empty? # true
array << 5 << 4 # [5, 4]
array.empty? # false

string = "" # empty line
string.empty? # true

blank? is Active Support specific method (available in any object) and is available in Ruby On Rails with Active Support. It returns true if an object is nil or empty.

Share:
37,749
Someth Victory
Author by

Someth Victory

I've been working extensively with Ruby on Rails for the last 4 years. I completed many projects with RoR, and API development for mobile, and offline application. I got strong knowledge in RoR, RSpec, Capybara, Postgresql, MySQL, MongoDB, AngularJS, Javascript, Coffeescript, SASS, Twitter Bootstrap, Git, Github. I do love best practices with OODesign, Design Patterns, TDD/BDD, Meta-Programming. I'm a serious Agile practitioner.

Updated on March 14, 2020

Comments

  • Someth Victory
    Someth Victory about 4 years

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

    Can anyone tell me the difference between nil?, blank? and empty? in Ruby?

  • fl00r
    fl00r almost 12 years
    blank? if ActiveSupport method
  • Ribtoks
    Ribtoks almost 12 years
    @fl00r yes, you're right. i'll edit the answer
  • V K Singh
    V K Singh over 4 years
    I have mistakenly downvote your answer, please edit some context so that I can revert back my downgrade vote!
  • mikej
    mikej over 4 years
    @VKSingh I've made a minor edit to the answer if you'd still like to revert your vote. Thank you!