Overriding nil?, curiosity

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 :slight_smile:

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

Matt Greer 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

···

--
Posted via http://www.ruby-forum.com/\.

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.

···

On 7/18/07, Stefan Rusterholz <apeiros@gmx.net> wrote:

Matt Greer 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.

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/