From: Robert Klemme Date: 2007-12-13T20:00:52+09:00 Subject: Re: Inheritance and meta-classes > > How do I find where a function is defined in the inheritance tree? > > Because of metaclass inheritance being cyclical I don't know where > > to stop looking. Where exactly do you see cycles? I am not aware of any. 2007/12/13, Adam Salter : > I still don't get how you can query and see where in the chain a > method got added to an object... > > I've now found that you can do: > obj.class.ancestors # => Array > > But that still doesn't include metaclasses... Correct. > How does Ruby know?? The meta class is there but it's hidden from #ancestors as you found out. Inheritance wise it sits between an object and its class. irb(main):001:0> c1 = Class.new => # irb(main):002:0> def c1.x; "X" end => nil irb(main):003:0> c1.x => "X" irb(main):004:0> c2 = Class.new c1 => # irb(main):005:0> c2.x => "X" irb(main):006:0> c2.ancestors => [#, #, Object, Kernel] irb(main):007:0> c1.class.instance_methods.grep /x/ => ["extend"] irb(main):008:0> class < ["x", "extend"] Note also that #respond_to? is a weak method to determine whether an instance will respond to a method, because of #method_missing which I believe is used a lot by Rails. Also, it is not called on the class but on the object. You can use #instance_method(s) to check whether a class implements a certain instance method but as I said, you might not be lucky. Kind regards robert