# Differnce between .nil? , .empty?, .blank?

**URL:** <https://www.ruby-forum.com/t/differnce-between-nil-empty-blank/143974>\
**Category:** Ruby\
**Created:** [July 24, 2008, 9:05am UTC](https://www.ruby-forum.com/t/differnce-between-nil-empty-blank/143974 "2008-07-24T09:05:47Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [July 24, 2008, 9:05am UTC](https://www.ruby-forum.com/t/differnce-between-nil-empty-blank/143974/1 "2008-07-24T09:05:47Z")

</div>

Hi  
Could you please tell me when to use .nil? , .empty?, .blank? .What  
are the difference between them… For example I have  
params[:company][:whichCompany]  
And to check for it is null I first attempted all these and finally the  
following worked  
if !params[:company][:whichCompany].empty?

So now really i am confused .Please tell me the differnce

Thanks in advance  
Sijo

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [July 24, 2008, 9:15am UTC](https://www.ruby-forum.com/t/differnce-between-nil-empty-blank/143974/2 "2008-07-24T09:15:31Z")

</div>

On Thu, Jul 24, 2008 at 9:00 AM, Sijo Kg [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Could you please tell me when to use .nil? , .empty?, .blank? .What  
> are the difference between them… For example I have  
> params[:company][:whichCompany]  
> And to check for it is null I first attempted all these and finally the  
> following worked  
> if !params[:company][:whichCompany].empty?
> 
> So now really i am confused .Please tell me the differnce

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: [Peak Obsession](http://api.rubyonrails.com/classes/Object.html#M000011).

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [July 24, 2008, 9:58am UTC](https://www.ruby-forum.com/t/differnce-between-nil-empty-blank/143974/3 "2008-07-24T09:58:40Z")

</div>

Hi  
Thanks for all your reply.Now I understood  
Sijo

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [July 24, 2008, 3:44pm UTC](https://www.ruby-forum.com/t/differnce-between-nil-empty-blank/143974/4 "2008-07-24T15:44:57Z")

</div>

Stefano C. wrote:

> Regarding blank?, I never heard of it

It’s a Rails method on the Object class that ‘combines’ the test for  
nil?  
and empty?. Simplifies ‘if thing.nil? || thing.empty?’ to ‘if  
thing.blank?’.

Best regards,  
Bill

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [July 24, 2008, 9:17am UTC](https://www.ruby-forum.com/t/differnce-between-nil-empty-blank/143974/5 "2008-07-24T09:17:17Z")

</div>

On Thursday 24 July 2008, Sijo Kg wrote:

> Thanks in advance  
> Sijo

.nil? tests whether the receiver is the nil object, that is the only  
instance  
of class NilClass, which is often used to indicate an invalid value.  
This  
method is defined in class Object, and thus is availlable for every  
object.

The other two methods, instead, are defined only for specific classes,  
so the  
answer depends. Usually, empty? is used to test whether an object is  
“empty”,  
for some class-depending meaning of empty. For example, String#empty?  
returns  
true if the string contains no characters, Array#empty? and Hash#empty?  
returns true if the array or hash has no entries. Other classes may  
define an  
empty? method in other ways. Note that, unlike nil?, empty? isn’t  
defined for  
all classes.

Regarding blank?, I never heard of it, so I can’t help you. You should  
look at  
the documentation of the class defining it.

Here are some examples about nil? and empty?

nil.nil?  
=\> true

false.nil?  
=\> false

1.nil?  
=\> false

0.nil?  
=\> false

“”.nil?  
=\> false

[].nil?  
=\> false

“”.empty?  
=\> true

“abc”.empty?  
=\> false

[].empty?  
=\> true

[1, 2, 3].empty?  
=\> false

1.empty?  
=\> NoMethodError

The last example means that the empty? method is not defined for class  
Fixnum

I hope this helps

Stefano

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [July 24, 2008, 7:20pm UTC](https://www.ruby-forum.com/t/differnce-between-nil-empty-blank/143974/6 "2008-07-24T19:20:49Z")

</div>

Hi Marc,

Marc H. wrote:

> Isn’t .nil? and .empty? in a way testing for a very similar  
> situation?

Not in Ruby. empty? is true for a String of length 0. The value of the  
String is not Nil. It turns out that Nil is very important in  
evaluations  
since it’s one of the two items in Ruby that evaluates to false.

irb(main):001:0\> str = String.new  
=\> “”  
irb(main):002:0\> str.length  
=\> 0  
irb(main):003:0\> if str then puts ‘a zero-length string is still a  
string’  
end  
a zero-length string is still a string  
=\> nil  
irb(main):004:0\> str = nil  
=\> nil  
irb(main):005:0\> if str.nil? then puts ‘assigning a nil value  
essentially  
kills  
the object’ end  
assigning a nil value essentially kills the object  
=\> nil.

HTH,  
Bill

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [July 24, 2008, 5:38pm UTC](https://www.ruby-forum.com/t/differnce-between-nil-empty-blank/143974/7 "2008-07-24T17:38:54Z")

</div>

Isn’t .nil? and .empty? in a way testing for a very similar situation?

Some instance var:  
@colour = ‘black’

Compared:  
@colour = ‘’  
@colour = nil

Which one to prefer of these?

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [July 24, 2008, 7:56pm UTC](https://www.ruby-forum.com/t/differnce-between-nil-empty-blank/143974/8 "2008-07-24T19:56:37Z")

</div>

blank? is not only a combination.  
blank? also test if there are printable characters in a string:

\n".empty?  
=\> false  
“\n”.blank?  
=\> true

Niklas H.

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [July 24, 2008, 8:18pm UTC](https://www.ruby-forum.com/t/differnce-between-nil-empty-blank/143974/9 "2008-07-24T20:18:54Z")

</div>

On Thu, Jul 24, 2008 at 7:49 PM, Niklas H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> blank? is not only a combination.  
> blank? also test if there are printable characters in a string:

More precisely any \S. Also false is blank? but the integer 0 is not.

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [July 15, 2010, 7:45am UTC](https://www.ruby-forum.com/t/differnce-between-nil-empty-blank/143974/10 "2010-07-15T07:45:03Z")

</div>

Xavier N. wrote:

> On Thu, Jul 24, 2008 at 9:00 AM, Sijo Kg [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Could you please tell me when to use .nil? , .empty?, .blank? .What  
> > are the difference between them… For example I have  
> > params[:company][:whichCompany]  
> > And to check for it is null I first attempted all these and finally the  
> > following worked  
> > if !params[:company][:whichCompany].empty?
> > 
> > So now really i am confused .Please tell me the differnce
> 
> 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: [Peak Obsession](http://api.rubyonrails.com/classes/Object.html#M000011).

This is answer was concise and helpful. Thanks!

I personally find this confusing:

> > a[:real]=false  
> > a[:real] =\> false  
> > a[:real].blank? =\> true ??

if I populated a[:real] with false, why does ‘.blank?’ return true?  
That’s confusing. But hey, if you can’t win them, join them. I love Ruby  
and Rails!

---

<div class="post-metadata">

**Author:** ![sijo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/da6949/32.png) [@sijo](https://www.ruby-forum.com/u/sijo)\
**Post date:** [July 15, 2010, 9:42am UTC](https://www.ruby-forum.com/t/differnce-between-nil-empty-blank/143974/11 "2010-07-15T09:42:56Z")

</div>

On Thu, Jul 15, 2010 at 7:45 AM, Michael N. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> I personally find this confusing:
> 
> > > a[:real]=false  
> > > a[:real] Â Â =\> false  
> > > a[:real].blank? Â Â =\> true Â Â ??
> 
> if I populated a[:real] with false, why does ‘.blank?’ return true?  
> That’s confusing. But hey, if you can’t win them, join them. I love Ruby  
> and Rails!

It should work

$ script/console  
Loading development environment (Rails 2.3.2)

> > a = {}  
> > =\> {}  
> > a[:real] = false  
> > =\> false  
> > a[:real].blank?  
> > =\> true

false is blank?, no matter where it is stored.
