In the "rubymonk" and some other ruby resources, it is mentioned that when you define a singleton method on an object, under the hood, Ruby is adding this new method to the object's metaclass. right? and also there is a trick to access the metaclass and it is it:
class Object
def metaclass
class << self
self
end
end
end
foo = "I'm a string object"
def foo.shout
puts self.upcase
end
foo.shout
p foo.metaclass.class
p foo.class.instance_methods.include? :shout
p foo.metaclass.instance_methods.include? :shout
and as we all expect, the result is:
I'M A STRING OBJECT
Class
false
true
and everything is ok. but what if we change the metaclass method to return Hash
instead of self
?
class Object
def metaclass
class << self
Hash
end
end
end
and then we check these things:
p foo.class.instance_methods.include? :shout
p foo.metaclass.instance_methods.include? :shout
p String.instance_methods.include? :shout
p Object.instance_methods.include? :shout
p Hash.instance_methods.include? :shout
and yeah, all of them are false:
false
false
false
false
false
the question is, what does shout
method belong to now? it is not the metaclass. so what is it?!