D
Daniel DeLorme
If I understand the ruby object model correctly, then an object's
singleton methods are defined as the instance methods of that object's
meta/singleton class. So I tried to confirm this:
obj = Object.new
obj_meta = (class << obj;self;end)
def obj.foobar; end
obj.singleton_methods #=> ["foobar"]
obj_meta.instance_methods(false) #=> ["foobar"]
So far so good. But if I try the same thing with a class:
class A; end
A_meta = (class << A;self;end)
def A.foobar; end
A.singleton_methods #=> ["foobar"]
A_meta.instance_methods(false) #=> ["allocate", "superclass", "foobar",
"new"]
I understand why those 3 extra methods appear, but in that case
shouldn't they also appear as part of A.singleton_methods? Furthermore
this does not happen with builtin classes. Does anyone have an
explanation for what is going on here?
Daniel
singleton methods are defined as the instance methods of that object's
meta/singleton class. So I tried to confirm this:
obj = Object.new
obj_meta = (class << obj;self;end)
def obj.foobar; end
obj.singleton_methods #=> ["foobar"]
obj_meta.instance_methods(false) #=> ["foobar"]
So far so good. But if I try the same thing with a class:
class A; end
A_meta = (class << A;self;end)
def A.foobar; end
A.singleton_methods #=> ["foobar"]
A_meta.instance_methods(false) #=> ["allocate", "superclass", "foobar",
"new"]
I understand why those 3 extra methods appear, but in that case
shouldn't they also appear as part of A.singleton_methods? Furthermore
this does not happen with builtin classes. Does anyone have an
explanation for what is going on here?
Daniel