From: daz Date: 2005-07-19T10:45:56+09:00 Subject: Re: How to find out a method's defining class ? 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 ? > Like Ara said, it doesn't seem to be provided :-( So, another (crude) hack ... #--------------------------------- class Method def definer_class inspect =~ /:\s(\w+)/ Object.const_get($1) end end #--------------------------------- tm = Time.method(:now) p tm #-> # p tm.definer_class #-> Time #--------------------------------- Internally the information we want is stored in an instance_variable called @__attached__ but it's not accessible from a script. (??) p tm.instance_variable_get(:@__attached__) p tm.class.instance_variable_get(:@__attached__) p (class << tm; self; end).instance_variable_get(:@__attached__) #-> nil # (all) daz