123

Is there any guidelines on how to differentiate between .nil?, .blank? and .empty??

I'm generally always confused as to when to use them in my application as they all seem to mean the same thing but have different meanings.

Does anyone have any cheat sheet on the gory details?

0

4 Answers 4

202

Here I made this useful table with all the casesenter image description here

2
  • nil.blank? returns undefined method error. I use object.to_s.empty?. It returns true when the object is nil or empty string. Oct 23, 2015 at 10:50
  • @SharvyAhmed object.to_s.empty? has one drawback when used in plain ruby - it raises NameError exception if object variable is not defined.
    – kode
    Feb 25, 2016 at 16:23
190
  • 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?.

4
  • 4
    String containing only "blank" characters is regarded as blank? but not empty?, e.g. " \n \t"
    – xhh
    Feb 28, 2013 at 8:21
  • 2
    Blank also returns true if the object has a value of false
    – riley
    Mar 8, 2013 at 4:19
  • I think the description for nil is inaccurate because nil.nil? is true, but nil is an object of NilClass.
    – mc9
    May 6, 2015 at 7:19
  • if you are (a) not using Rails but (b) are using ActiveSupport, at least above some version (not sure which) ... and you need to include blank? only, here is how to cherry-pick just this one bit: require 'active_support/core_ext/object/blank (source)
    – floer32
    Oct 11, 2016 at 4:44
7
  • nil? is defined on all Objects, it only returns true on the nil singleton.

  • blank? is defined on all objects too, it returns true if the object also responds to empty? and is empty, or is a false type value (!object is always true).

  • empty? is defined on several collection objects, and is true if it has no elements. It is also defined on String.

note that blank? is ActiveSupport and not in Rails 1.8.

6

I found a good explanation here:

nil? tests whether the object is exactly nil, that is whether it is the one and only want instance of NilClass.

empty? is a method some objects respond to. You need to check the documentation for each case. For example, and empty array is one that is not nil (it is an array right?) and has no elements. An empty string is one that is not nil (it is a string right?) and has no bytes, nothing.

The blank? method you ask for does not belong to Ruby, it is a Rails extension: http://api.rubyonrails.com/classes/Object.html#M000011.

If you click through to the link at the end of that post you will find that the blank? method simply combines the nil? and empty? method calls.

1
  • String containing only "blank" characters is regarded as blank? but not empty?, e.g. " \n \t", as @xhh wrote in another answer to this question.
    – ANeves
    Sep 17, 2013 at 0:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.