122

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?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
David C
  • 3,659
  • 5
  • 34
  • 46

4 Answers4

201

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

Julian Popov
  • 17,401
  • 12
  • 55
  • 81
  • 14
    This table is excellent- thanks for sharing! – BTHarris Feb 06 '14 at 18:37
  • `nil.blank?` returns `undefined method` error. I use `object.to_s.empty?`. It returns `true` when the object is `nil` or `empty` string. – Sharvy Ahmed Oct 23 '15 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 '16 at 16:23
189
  • 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?.

Ilya
  • 13,337
  • 5
  • 37
  • 53
Karmen Blake
  • 3,454
  • 3
  • 18
  • 9
  • 4
    String containing only "blank" characters is regarded as `blank?` but not `empty?`, e.g. " \n \t" – xhh Feb 28 '13 at 08:21
  • 2
    Blank also returns true if the object has a value of false – riley Mar 08 '13 at 04:19
  • I think the description for `nil` is inaccurate because `nil.nil?` is true, but `nil` is an object of `NilClass`. – mc9 May 06 '15 at 07: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](http://guides.rubyonrails.org/active_support_core_extensions.html)) – floer32 Oct 11 '16 at 04: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.

AdrieanKhisbe
  • 3,899
  • 8
  • 37
  • 45
cwninja
  • 9,550
  • 1
  • 29
  • 22
5

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.

Andrew Hare
  • 344,730
  • 71
  • 640
  • 635
  • `String containing only "blank" characters is regarded as blank? but not empty?, e.g. " \n \t"`, as [@xhh](http://stackoverflow.com/questions/1654960/when-to-use-nil-blank-empty#comment21296385_1655001) wrote in another answer to this question. – ANeves Sep 17 '13 at 00:07