ruby - what does a singleton method belong to if the metaclass method is wrongly overridden? -


in "rubymonk" , other ruby resources, mentioned when define singleton method on object, under hood, ruby adding new method object's metaclass. right? , there trick access metaclass , it:

class object   def metaclass     class << self       self     end   end end  foo = "i'm 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 expect, result is:

i'm string object class false true 

and ok. if change metaclass method return hash instead of self?

  class object       def metaclass         class << self           hash         end       end     end 

and 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, of them false:

false false false false false 

the question is, shout method belong now? not metaclass. it?!

still metaclass, you've removed ability access directly...

foo.instance_eval { class << self; self; end.instance_methods.include?(:shout) }   => true 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -