Adam Salter
12/13/2007 5:00:00 AM
Any suggestions?
(or has the subject been done to death...)
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...
How does Ruby know??
On 13/12/2007, at 10:15 AM, Adam Salter wrote:
> Hi all,
>
> OK. I'm trying to work out where a method is defined in Rails.
> It's in a test, and the specific function that's being called is
> 'post', but my question is not a Rails question, it's Ruby.
>
> 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.
>
> I made the following as a test but I still can't find the function
> being called so I'm guessing I'm missing some of the inheritance
> tree...
>
> module Debugs
> def find_respond_to_in_class_heirarchy(obj, method)
> if obj.respond_to?(:superclass)
> arr = find_respond_to_in_class_heirarchy(obj.superclass, method)
> arr.unshift([obj.class.respond_to?(method),
> obj.metaclass.respond_to?(method), obj.superclass.respond_to?
> (method)])
> elsif obj.nil?
> arr = [[nil.class.respond_to?(method), nil.metaclass.respond_to?
> (method), NoMethodError]]
> else
> arr = find_respond_to_in_class_heirarchy(obj.class, method)
> arr.unshift([obj.class.respond_to?(method),
> obj.metaclass.respond_to?(method), NoMethodError])
> end
> arr
> end
> end
>
> class Object
> include Debugs
> def metaclass
> class << self
> self
> end
> end
> end
>
> Wow this whole metaclass, class, object, module relationship is
> pretty mindbending...
>
> Cheers,
> -Adam
>