Forum: Ruby overriding nil?, curiosity

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Matt G. (Guest)
on 2007-07-19 02:29
(Received via mailing list)
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.
Stefan R. (Guest)
on 2007-07-19 03:39
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
Rick D. (Guest)
on 2007-07-20 21:09
(Received via mailing list)
On 7/18/07, Stefan R. <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/
This topic is locked and can not be replied to.