# Nil? and blank?

**URL:** <https://www.ruby-forum.com/t/nil-and-blank/116800>\
**Category:** Rails\
**Created:** [October 9, 2007, 10:03pm UTC](https://www.ruby-forum.com/t/nil-and-blank/116800 "2007-10-09T22:03:58Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [October 9, 2007, 10:03pm UTC](https://www.ruby-forum.com/t/nil-and-blank/116800/1 "2007-10-09T22:03:58Z")

</div>

I’ve heard this question asked before but I couldn’t find the  
appropriate responces that I was looking for, but what is the difference  
between Object.nil? and Object.blank? Is it how one handles white and  
newline characters? To me they almost seem the same but I imagine there  
has to be some differences. Thanks,

-S

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [October 9, 2007, 10:21pm UTC](https://www.ruby-forum.com/t/nil-and-blank/116800/2 "2007-10-09T22:21:16Z")

</div>

An object is blank if it’s nil, empty, or a whitespace string. If it’s  
nil, it’s nil.

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [October 10, 2007, 2:27pm UTC](https://www.ruby-forum.com/t/nil-and-blank/116800/3 "2007-10-10T14:27:27Z")

</div>

blank is:

“”  
" "  
nil  
[]  
{}  
false

any of the above applied with .blank? will return true.

“”.blank? # returns true  
[].blank? # returns true  
{""=\>""}.blank? # returns false  
{}.blank? # returns true

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [October 9, 2007, 10:51pm UTC](https://www.ruby-forum.com/t/nil-and-blank/116800/4 "2007-10-09T22:51:47Z")

</div>

On Oct 9, 2007, at 4:03 PM, Shandy N. wrote:

> I’ve heard this question asked before but I couldn’t find the  
> appropriate responces that I was looking for, but what is the  
> difference  
> between Object.nil? and Object.blank? Is it how one handles white and  
> newline characters? To me they almost seem the same but I imagine  
> there  
> has to be some differences. Thanks,
> 
> -S

Use the Source…  
(vendor/rails/activesupport/lib/active\_support/core\_ext/blank.rb)

class Object #:nodoc:

# “”, " ", nil, [], and {} are blank

def blank?  
if respond\_to?(:empty?) && respond\_to?(:strip)  
empty? or strip.empty?  
elsif respond\_to?(:empty?)  
empty?  
else  
!self  
end  
end  
end

class NilClass #:nodoc:  
def blank?  
true  
end  
end

class FalseClass #:nodoc:  
def blank?  
true  
end  
end

class TrueClass #:nodoc:  
def blank?  
false  
end  
end

class Array #:nodoc:  
alias\_method :blank?, :empty?  
end

class Hash #:nodoc:  
alias\_method :blank?, :empty?  
end

class String #:nodoc:  
def blank?  
empty? || strip.empty?  
end  
end

class Numeric #:nodoc:  
def blank?  
false  
end  
end

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid  
+1 513-295-4739  
Skype: rob.biedenharn

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [October 10, 2007, 3:27pm UTC](https://www.ruby-forum.com/t/nil-and-blank/116800/5 "2007-10-10T15:27:29Z")

</div>

Thanks all, that helps out a lot.

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [November 3, 2007, 6:22am UTC](https://www.ruby-forum.com/t/nil-and-blank/116800/6 "2007-11-03T06:22:42Z")

</div>

> Use the Source…  
> (vendor/rails/activesupport/lib/active\_support/core\_ext/blank.rb)

I’d like to have a .blank? for Time and date where 0000-00-00 00:00:00  
etc is considered blank.

Where’s the right place w/in a Rails project to add an extension to  
Ruby’s classes to accomplish that?

– gw

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [November 3, 2007, 7:01pm UTC](https://www.ruby-forum.com/t/nil-and-blank/116800/7 "2007-11-03T19:01:22Z")

</div>

On Nov 3, 2007, at 5:35 AM, Rob B. wrote:

> Well, for my projects, I’ve started putting these kinds of things into  
> a file in:  
> RAILS\_ROOT/lib/ext/some\_class\_or\_behavior\_name.rb  
> and I require them in my config/environment.rb like this:

Makes sense. Thanks.

– gw

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [November 3, 2007, 1:36pm UTC](https://www.ruby-forum.com/t/nil-and-blank/116800/8 "2007-11-03T13:36:01Z")

</div>

On Nov 3, 2007, at 1:22 AM, Greg W. wrote:

> > Use the Source…  
> > (vendor/rails/activesupport/lib/active\_support/core\_ext/blank.rb)
> 
> I’d like to have a .blank? for Time and date where 0000-00-00 00:00:00  
> etc is considered blank.
> 
> Where’s the right place w/in a Rails project to add an extension to  
> Ruby’s classes to accomplish that?
> 
> – gw

Well, for my projects, I’ve started putting these kinds of things into  
a file in:

RAILS\_ROOT/lib/ext/some\_class\_or\_behavior\_name.rb

and I require them in my config/environment.rb like this:

Dir[File.join(RAILS\_ROOT, ‘lib’, ‘ext’, ‘\*.rb’)].each do |f|  
base = File.basename(f, ‘.rb’)  
require “ext/#{base}”  
end

One project, for example, has files:  
active\_record\_compare.rb  
array.rb  
enumerable.rb  
file.rb  
integer.rb  
nil\_class.rb  
range.rb  
string.rb  
web\_agent.rb

And my nil\_class.rb contains:  
class NilClass

# Allowing a chain like: value.nonblank? || ‘default value’

def nonblank?  
self  
end

# so it plays nicely with Numeric#nonzero?

def nonzero?  
self  
end  
end

That should give you some ideas.

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid
