How to use "method_missing"?

X

Xiangrong Fang

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
 
T

ts

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

you have defined a singleton method for the object NilClass

X> 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

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

you have defined a singleton method for the object a

X> 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
 
X

Xiangrong Fang

Hi Guy,

X> 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
 
G

George Ogata

Xiangrong Fang said:
Hi Guy,



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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top