# Subclassing NilClass

**URL:** <https://www.ruby-forum.com/t/subclassing-nilclass/47794>\
**Category:** Ruby\
**Created:** [November 13, 2005, 11:59pm UTC](https://www.ruby-forum.com/t/subclassing-nilclass/47794 "2005-11-13T23:59:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![listrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f17d59/32.png) [@listrecv](https://www.ruby-forum.com/u/listrecv)\
**Post date:** [November 13, 2005, 11:59pm UTC](https://www.ruby-forum.com/t/subclassing-nilclass/47794/1 "2005-11-13T23:59:32Z")

</div>

I’d like to subclass NilClass, creating a NullCase, implementing in  
Ruby a simple example of Martin F.'s “SpecialCase” pattern (  
[http://patternshare.org/default.aspx/Home.MF.SpecialCase](http://patternshare.org/default.aspx/Home.MF.SpecialCase) )

I’d like to subclass NilClass, so that the object returns false (and  
nil? returns true). But, I’d like to override method\_missing so that  
it returns the an instance of NullCase - not an exception, and not a  
regular nil.

I had trouble subclassing NilClass - there’s no new method - how do I  
do this?

Basically, usage would be like

def a\_method  
…  
if condition  
x  
else  
NULL\_CASE  
end  
end

Later, you could do:  
a = o.a\_method  
a.address.zip #–\> No errors, just nil’s

instead of  
if a && a.address && a.address.zip  
a.address.zip  
else  
nil  
end

Please cc all responses to me at listrecv at gmail, thanks!

---

<div class="post-metadata">

**Author:** ![listrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f17d59/32.png) [@listrecv](https://www.ruby-forum.com/u/listrecv)\
**Post date:** [November 14, 2005, 12:20am UTC](https://www.ruby-forum.com/t/subclassing-nilclass/47794/2 "2005-11-14T00:20:34Z")

</div>

On 11/13/05, removed\_email\_address@domain.invalid [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I had trouble subclassing NilClass - there’s no new method - how do I  
> do this?

You don’t. See the thread beginning with [ruby-talk:157101].

[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/157101](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/157101)

---

<div class="post-metadata">

**Author:** ![listrecv](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f17d59/32.png) [@listrecv](https://www.ruby-forum.com/u/listrecv)\
**Post date:** [November 14, 2005, 2:17am UTC](https://www.ruby-forum.com/t/subclassing-nilclass/47794/3 "2005-11-14T02:17:45Z")

</div>

On Nov 13, 2005, at 5:57 PM, removed\_email\_address@domain.invalid wrote:

> Later, you could do:  
> a = o.a\_method  
> a.address.zip #–\> No errors, just nil’s
> 
> instead of  
> if a && a.address && a.address.zip  
> a.address.zip  
> else  
> nil  
> end

Take a look at the NullObject pattern: [http://c2.com/cgi/wiki?NullObject](http://c2.com/cgi/wiki?NullObject)  
you might be able to come up with some specific null objects for your  
scenario.

I’d shoot for something specific to you problem but Ruby of course  
will let you create a “generic” null object:

class Object  
def mynull?  
false  
end  
end

class MyNull  
def method\_missing(name, \*args, &block)  
self  
end

def mynull?  
true  
end

def to\_s  
“”  
end  
end

You can’t depend on the object testing as false, use #mynull? if  
you need to know what it is.

Gary W.
