How to use "method_missing"?

Hi All,

I try to us the following method to prevent some runtime errors:

irb(main):001:0> def NilClass.method_missing(m, *a)
irb(main):002:1> return nil
irb(main):003:1> end
=> nil
irb(main):004:0> a = []
=> []
irb(main):005:0> a[1]
=> nil
irb(main):006:0> a[1].length
NoMethodError: undefined method `length' for nil:NilClass
        from (irb):7

You can see that even I let NilClass's method_missing to return nil, it
still report "NoMethodError". I have also tested the following:

irb(main):006:0> a = nil
=> nil
irb(main):007:0> def a.method_missing(m, *a)
irb(main):008:1> return nil
irb(main):009:1> end
=> nil
irb(main):010:0> a.hello
=> nil

This time, it seems worked. Could anyone enlight me please.

Thanks!
Shannon

irb(main):001:0> def NilClass.method_missing(m, *a)

  you have defined a singleton method for the object NilClass

NoMethodError: undefined method `length' for nil:NilClass

ruby search the method #length for the object nil, it then search the
method #method_missing for the object nil and don't find it

irb(main):007:0> def a.method_missing(m, *a)

you have defined a singleton method for the object a

irb(main):010:0> a.hello

ruby search the method #length for the object a, it then search the
method #method_missing for the object a and it find it

conclusion : define the method #method_missing for nil

   class NilClass
      def method_missing(*a)
         nil
      end
   end

Guy Decoux

Hi Guy,

> irb(main):001:0> def NilClass.method_missing(m, *a)

  you have defined a singleton method for the object NilClass

I know I defined a singleton method for object a, but I don't quite
understand why I am defining a singleton for NilClass, by doing

def NilClass.method_missing...

Can NilClass be an object name rather than the Class NilClass? If I am
defining a singleton method, then, does the OBJECT NilClass has to exist
before I define the singleton method?

thanks,
Shannon

···

On Sun, 6 Jun 2004 19:31:56 +0900 ts <decoux@moulon.inra.fr> wrote:

Xiangrong Fang <xrfang@hotmail.com> writes:

Hi Guy,

> irb(main):001:0> def NilClass.method_missing(m, *a)

  you have defined a singleton method for the object NilClass

I know I defined a singleton method for object a,

You have defined a singleton method for the object NilClass, actually.
But it sounds like you knew that.

but I don't quite
understand why I am defining a singleton for NilClass, by doing

def NilClass.method_missing...

Can NilClass be an object name rather than the Class NilClass? If I am
defining a singleton method, then, does the OBJECT NilClass has to exist
before I define the singleton method?

Yes it must exist (ruby creates it before running your program). No,
NilClass can't refer to anything else unless you assign something else
to it first (NilClass is a constant just like any other capitalized
identifier), which is almost certainly a silly thing to do.

If you really insist, though, you can reassign it thusly:

  Object.const_set('NilClass', 100)

HTH

···

On Sun, 6 Jun 2004 19:31:56 +0900 > ts <decoux@moulon.inra.fr> wrote: