# Extending nil methods

**URL:** <https://www.ruby-forum.com/t/extending-nil-methods/109803>\
**Category:** Rails\
**Created:** [July 19, 2007, 6:39am UTC](https://www.ruby-forum.com/t/extending-nil-methods/109803 "2007-07-19T06:39:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Reynard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/6bbea6/32.png) [@Reynard](https://www.ruby-forum.com/u/Reynard)\
**Post date:** [July 19, 2007, 6:39am UTC](https://www.ruby-forum.com/t/extending-nil-methods/109803/1 "2007-07-19T06:39:28Z")

</div>

has anyone done something like this?

class NilClass  
def empty?; true; end  
def strip; nil; end  
def ; nil; end  
end

which is nice so that you can do this without throwing exception:  
if !params[:something].empty?  
if !params[:something][:else].strip.empty?

as opposed to this:  
if params[:something] && !params[:something].empty?  
if params[:something] && params[:something][:else] && !  
params[:something][:else].strip.empty?

It seems to work fine for me so far. I’m just wondering what’s the  
negative effect of doing that? If not then shouldn’t we be doing  
this 🙂 ?

- reynard

---

<div class="post-metadata">

**Author:** ![Reynard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/6bbea6/32.png) [@Reynard](https://www.ruby-forum.com/u/Reynard)\
**Post date:** [July 19, 2007, 10:57am UTC](https://www.ruby-forum.com/t/extending-nil-methods/109803/2 "2007-07-19T10:57:02Z")

</div>

Reynard wrote:

> has anyone done something like this?
> 
> class NilClass  
> def empty?; true; end  
> def strip; nil; end  
> def ; nil; end  
> end
> 
> which is nice so that you can do this without throwing exception:  
> if !params[:something].empty?  
> if !params[:something][:else].strip.empty?
> 
> as opposed to this:  
> if params[:something] && !params[:something].empty?  
> if params[:something] && params[:something][:else] && !  
> params[:something][:else].strip.empty?
> 
> It seems to work fine for me so far. I’m just wondering what’s the  
> negative effect of doing that? If not then shouldn’t we be doing  
> this 🙂 ?
> 
> - reynard

You disable errors that make perfect sense. The problem is that you  
really get into a world of pain if you do not expect any nil values is  
you Hash (or the expected Hash to be nil instead). This is when you want  
to see those errors.

I see your problem with if params[:something][:else], but your first  
example is already solved by such a technique: Rails has #blank? which  
is defined on nil.

if params[:something] && !params[:something].empty?  
=\>  
unless params[:something].blank?

---

<div class="post-metadata">

**Author:** ![Reynard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/6bbea6/32.png) [@Reynard](https://www.ruby-forum.com/u/Reynard)\
**Post date:** [July 19, 2007, 11:04am UTC](https://www.ruby-forum.com/t/extending-nil-methods/109803/3 "2007-07-19T11:04:27Z")

</div>

I agree,  
blank? is the way.

otherwise I suggest;

class NilClass  
def method\_missing(method\_name, \*args)  
return true  
end  
end

Florian G. wrote:

> Reynard wrote:
> 
> > has anyone done something like this?
> > 
> > class NilClass  
> > def empty?; true; end  
> > def strip; nil; end  
> > def ; nil; end  
> > end

---

<div class="post-metadata">

**Author:** ![Reynard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/6bbea6/32.png) [@Reynard](https://www.ruby-forum.com/u/Reynard)\
**Post date:** [July 19, 2007, 5:56pm UTC](https://www.ruby-forum.com/t/extending-nil-methods/109803/4 "2007-07-19T17:56:10Z")

</div>

ah I knew there must be a better way. It’s strange that I cannot find  
blank? in the ruby or rails api documentation.  
Thanks a lot,

- reynard

On Jul 19, 5:04 am, Matthew R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
