# Overriding nil?, curiosity

**URL:** <https://www.ruby-forum.com/t/overriding-nil-curiosity/109776>\
**Category:** Ruby\
**Created:** [July 19, 2007, 12:29am UTC](https://www.ruby-forum.com/t/overriding-nil-curiosity/109776 "2007-07-19T00:29:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![javamllama](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@javamllama](https://www.ruby-forum.com/u/javamllama)\
**Post date:** [July 19, 2007, 12:29am UTC](https://www.ruby-forum.com/t/overriding-nil-curiosity/109776/1 "2007-07-19T00:29:49Z")

</div>

Maybe this is a dumb question, but I’m gonna give it a shot.

I was showing my friends how Ruby classes are always open and I did some  
dumb things like redefine Integer#+ to always return 5, and such. But  
trying  
this:

NilClass.class\_eval do  
def nil?  
false  
end  
end

result in this:

irb(main):008:0\> nil  
/opt/local/lib/ruby/1.8/irb/slex.rb:235:in `match_io': undefined method`call’ for nil:NilClass (NoMethodError)  
from /opt/local/lib/ruby/1.8/irb/slex.rb:76:in `match' from /opt/local/lib/ruby/1.8/irb/ruby-lex.rb:287:in`token’  
from /opt/local/lib/ruby/1.8/irb/ruby-lex.rb:263:in `lex' from /opt/local/lib/ruby/1.8/irb/ruby-lex.rb:234:in`each\_top\_level\_statement’  
from /opt/local/lib/ruby/1.8/irb/ruby-lex.rb:230:in `loop' from /opt/local/lib/ruby/1.8/irb/ruby-lex.rb:230:in`each\_top\_level\_statement’  
from /opt/local/lib/ruby/1.8/irb/ruby-lex.rb:229:in `catch' from /opt/local/lib/ruby/1.8/irb/ruby-lex.rb:229:in`each\_top\_level\_statement’  
from /opt/local/lib/ruby/1.8/irb.rb:146:in `eval_input' from /opt/local/lib/ruby/1.8/irb.rb:70:in`start’  
from /opt/local/lib/ruby/1.8/irb.rb:69:in `catch' from /opt/local/lib/ruby/1.8/irb.rb:69:in`start’  
from /opt/local/bin/irb:13

Taking a look at the code and I’m completely lost. Obviously Ruby itself  
depends on a usable definition of nil? I’m sure it depends on some key  
methods in String, Array etc to be defined as expected too. So I guess  
there  
are some exceptions where ruining a method ruins Ruby itself making it  
so  
you never get a chance to ruin your own code 🙂

Like I said, dumb question and just a curiosity, my apologies if this  
isn’t  
very relevant.

---

<div class="post-metadata">

**Author:** ![javamllama](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@javamllama](https://www.ruby-forum.com/u/javamllama)\
**Post date:** [July 19, 2007, 1:39am UTC](https://www.ruby-forum.com/t/overriding-nil-curiosity/109776/2 "2007-07-19T01:39:13Z")

</div>

Matt G. wrote:

> Maybe this is a dumb question, but I’m gonna give it a shot.
> 
> I was showing my friends how Ruby classes are always open and I did some  
> dumb things like redefine Integer#+ to always return 5, and such. But  
> trying  
> this:
> 
> NilClass.class\_eval do  
> def nil?  
> false  
> end  
> end
> 
> result in this: \<irb code, snip\>  
> Taking a look at the code and I’m completely lost. Obviously Ruby itself  
> depends on a usable definition of nil? I’m sure it depends on some key  
> methods in String, Array etc to be defined as expected too. So I guess  
> there

Nope, not ruby. IRB. Because IRB is written in ruby.

Regards  
Stefan

---

<div class="post-metadata">

**Author:** ![javamllama](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ce7236/32.png) [@javamllama](https://www.ruby-forum.com/u/javamllama)\
**Post date:** [July 20, 2007, 7:09pm UTC](https://www.ruby-forum.com/t/overriding-nil-curiosity/109776/3 "2007-07-20T19:09:48Z")

</div>

On 7/18/07, Stefan R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ```
> > false
> > 
> > ```
> > 
> > end  
> > end
> > 
> > result in this: \<irb code, snip\>  
> > Taking a look at the code and I’m completely lost. Obviously Ruby itself  
> > depends on a usable definition of nil? I’m sure it depends on some key  
> > methods in String, Array etc to be defined as expected too. So I guess  
> > there
> 
> Nope, not ruby. IRB. Because IRB is written in ruby.

Back in 1983, the first widely avaiable official documentation on  
Smalltalk was published as four volumes, one on the language and it’s  
implementaiton, one on the development environment, one on writing  
graphical applications, and the fourth called “Bits of History, Words  
of Advice” which captured various experiences of those who had  
implemented and used Smalltalk up to that point.

The last volume is illustrated by many cartoons, one of my favorites  
comes early on and consists of a completely black frame with a word  
balloon containing “OOPS! I just typed ‘Processor ← nil.’!”

Languages like Ruby and Smaltalk give you great power to change the  
floor you’re standing on, but as always, with great power comes great  
responsibility.

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
