> 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?
You have 2 objects in memory:
1. an Object of type Class named 'NilClass'
2. an object of type NilClass named 'nil'
def NilClass.method ...
Defines a method on the 1st object. This can then be called by
NilClass.method (ie it's a class-method).
def nil.method defines the object on the 2nd object. Can then be called
as nil.method. This is the same for any object. It's part of the
singleton class for that particular object.
class NilClass
def method ...
defines a method for all instanced of NilClass (all objects of 'type'
NilClass). In the case of nil there is only one instance (a singleton)
which is named 'nil'. If you would do it for MyClass it would define the
method for all instances of MyClass. The method would then be called as
'MyClass.new.method...'