I got this question from this discussion. A method call like object.m
does not always mean the class of "object" has a "m" method, just like the find method to a Array object is not directly originated from Array object, but from the mixed-in Enumerable module. My question is, given a method, how can we determine the class from which the method originated?
views:
167answers:
5
+1
A:
I'm thinking something like this could work
def print_ancestor_definitions(cl,method)
ancestors = cl.ancestors
p ancestors.join(' < ') #Print heirarchy
p "Searching..."
ancestors.each do |c|
if c.instance_methods.include? method
p "#{c} defines #{method} as an instance method!"
elsif c.singleton_methods.include? method
p "#{c} defines #{method} as a singleton method"
else
p "#{c} doesn't define #{method}"
end
end
end
print_ancestor_definitions(Array,'find')
# >> "Array < Enumerable < Object < Kernel"
# >> "Searching..."
# >> "Array defines find as an instance method!"
# >> "Enumerable defines find as an instance method!"
# >> "Object doesn't define find"
# >> "Kernel doesn't define find"
I suppose the last one to have the method is the one who defines it?
dylanfm
2009-01-22 06:25:25
A:
I'm not sure we can precisely find where a method come from, when you include a mixin, all the methods become part of your class as if you did put them there. See answer from dylanfm for an approx.
Keltia
2009-01-22 06:27:55
method.inspect will tell you if a method is attached to a Mixin
Toby Hede
2009-01-22 06:30:28
+8
A:
Any class/object method is an object in Ruby, and has some methods of it's own.
So you can do this:
[].method(:count).inspect
=> "#<Method: Array#count>"
[].method(:detect).inspect
=> "#<Method: Array(Enumerable)#detect>"
Quick bit of RegEx and you're done.
Toby Hede
2009-01-22 06:28:57
+5
A:
tobyhede's answer is awesome, but I just did a bit of digging in irb
and there's no need to slice up the output of #inspect
. The Method
class
>> Object.new.method(:inspect)
=> #<Method: Object(Kernel)#inspect>
has some useful methods of its own:
>> Object.new.method(:inspect).methods - Object.methods
=> ["owner", "call", "to_proc", "unbind", "arity", "receiver", "[]"]
In particular the #owner
method, which returns the owner as a proper object:
>> [].method(:count).owner
=> Array
>> [].method(:detect).owner
=> Enumerable
ben_h
2009-02-01 11:57:26