From: "Ara.T.Howard" Date: 2005-07-19T08:45:39+09:00 Subject: Re: How to find out a method's defining class ? On Tue, 19 Jul 2005, Rolf Gebauer wrote: > Hi, > > 1. a method created with Classname.method( 'method_name' ) > knows the class it's defined for, as inspect shows. > > Object.methods returns an Array of Strings only. > > ObjectSpace.each_object( Method ) delivers methods created > as in 1. only > > Does Reflection provides a way to find out ? > > Thanks in advance i don't know a direct way, but maybe: harp:~ > cat a.rb class Object def defines method singleton_class = class << self; self; end ([singleton_class] + self.class.ancestors).reverse.each do |a| return a if a.instance_methods.include? method end return nil end alias defines? defines end module M def foobar; end end class C def foo; end end class B < C include M def bar; end end obj = B::new p(obj.defines('foo')) p(obj.defines('bar')) p(obj.defines('foobar')) a = [] class << a def singleton_method; end end p(a.defines('each')) p(a.defines('singleton_method')) p(a.defines('not_defined')) harp:~ > ruby a.rb C B M Array #> nil note that this is just an idea - but it may suit your purposes. hth. -a -- =============================================================================== | email :: ara [dot] t [dot] howard [at] noaa [dot] gov | phone :: 303.497.6469 | My religion is very simple. My religion is kindness. | --Tenzin Gyatso ===============================================================================