Ruby Class and Object singleton - get access -
i learning ruby singletons , have misunderstanding such code:
class myclass def self.class_singleton_mymethod end end class_singleton = class << myclass self end puts class_singleton.methods.grep(/mymethod/) # => [] obj = myclass.new def obj.object_singleton_mymethod end object_singleton = class << obj self end puts object_singleton.methods.grep(/mymethod/) # => class_singleton_mymethod
why class_singleton not contains class's class method , object_singleton instead of object's singleton method contains class's class method?
i think have notion of methods
, instance_methods
mixed up. if replace instances of methods
instance_methods
, see results expect.
instance_methods
used enumerate methods class's instances have. methods
used enumerate methods object has. (class objects objects too, , have own methods new
not instance methods.
for example, string#slice
instance method; can call slice
on string instances. on other hand, string.new
method on string
itself; don't call new
on string instances, can call string.new
(i.e., on string
class object itself) create new string.)
Comments
Post a Comment