From: Dominik Bathon Date: 2005-12-23T13:00:27+09:00 Subject: Re: How do you know what class defines method foo()? On Fri, 23 Dec 2005 00:12:51 +0100, Logan Capaldo wrote: > Array.method_defined?(:partition) > #=> true > > Enumerable.method_defined?(:partition) > #=> true > > Same for instance_methods.include?. > If we loop forward through ancestors, we get Array as the answer. If we > loop backwards we get Enumerable. The problem is, looping backwards will > get us the wrong answer if Array overrides partition. I can't think of a > good why to do this unless there is a Class#overrides? method. (e.g. > Array.overrides?(:partition) #=> false ). I just played a bit around and found Method#inspect: >> a = [] => [] >> a.method(:partition) => # >> a.method(:first) => # >> a.method(:send) => # >> class Array; def send *a; super end end => nil >> a.method(:send) => # So, if just want to know it for one case, use irb and look at Method#inspect. To write a method that returns the class/module that defines a method one might parse the output of Method#inspect, but I think that is not very nice. There doesn't seem to be a direct way to get the class/module in Ruby. Dominik