# How to find out a method's defining class?

**URL:** <https://rubytalk.org/t/how-to-find-out-a-methods-defining-class/19610>\
**Category:** ruby-talk\
**Created:** [18 July 2005 22:20 UTC](https://rubytalk.org/t/how-to-find-out-a-methods-defining-class/19610 "2005-07-18T22:20:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rolf\_Gebauer](https://avatars.discourse-cdn.com/v4/letter/r/8491ac/32.png) [@Rolf\_Gebauer](https://rubytalk.org/u/Rolf_Gebauer)\
**Post date:** [18 July 2005 22:20 UTC](https://rubytalk.org/t/how-to-find-out-a-methods-defining-class/19610/1 "2005-07-18T22:20:55Z")

</div>

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  
rg

---

<div class="post-metadata">

**Author:** ![Ara.T.Howard6](https://avatars.discourse-cdn.com/v4/letter/a/958977/32.png) [@Ara.T.Howard6](https://rubytalk.org/u/Ara.T.Howard6)\
**Post date:** [18 July 2005 23:45 UTC](https://rubytalk.org/t/how-to-find-out-a-methods-defining-class/19610/2 "2005-07-18T23:45:39Z")

</div>

i don't know a direct way, but maybe:

&nbsp;&nbsp;&nbsp;harp:~ \> cat a.rb  
&nbsp;&nbsp;&nbsp;class Object  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def defines method  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;singleton\_class = class \<\< self; self; end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;([singleton\_class] + self.class.ancestors).reverse.each do |a|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return a if a.instance\_methods.include? method  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return nil  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alias defines? defines  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;module M  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def foobar; end  
&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;class C  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def foo; end  
&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;class B \< C  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;include M  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def bar; end  
&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;obj = B::new  
&nbsp;&nbsp;&nbsp;p(obj.defines('foo'))  
&nbsp;&nbsp;&nbsp;p(obj.defines('bar'))  
&nbsp;&nbsp;&nbsp;p(obj.defines('foobar'))

&nbsp;&nbsp;&nbsp;a =   
&nbsp;&nbsp;&nbsp;class \<\< a  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def singleton\_method; end  
&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;p(a.defines('each'))  
&nbsp;&nbsp;&nbsp;p(a.defines('singleton\_method'))  
&nbsp;&nbsp;&nbsp;p(a.defines('not\_defined'))

&nbsp;&nbsp;&nbsp;harp:~ \> ruby a.rb  
&nbsp;&nbsp;&nbsp;C  
&nbsp;&nbsp;&nbsp;B  
&nbsp;&nbsp;&nbsp;M  
&nbsp;&nbsp;&nbsp;Array  
&nbsp;&nbsp;&nbsp;#\<Class:#\<Array:0xb75d1db4\>\>  
&nbsp;&nbsp;&nbsp;nil

note that this is just an idea - but it may suit your purposes.

hth.

-a

> **···**
>
> 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
> 
> # --
> 
> > 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
> 
> ===============================================================================

---

<div class="post-metadata">

**Author:** ![daz](https://avatars.discourse-cdn.com/v4/letter/d/87869e/32.png) [@daz](https://rubytalk.org/u/daz)\
**Post date:** [19 July 2005 01:45 UTC](https://rubytalk.org/t/how-to-find-out-a-methods-defining-class/19610/3 "2005-07-19T01:45:56Z")

</div>

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  
> &nbsp;&nbsp;&nbsp;def definer\_class  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inspect =~ /:\s(\w+)/  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Object.const\_get($1)  
> &nbsp;&nbsp;&nbsp;end  
> end  
> #---------------------------------  
> tm = Time.method(:now)  
> p tm #-\> #\<Method: Time.now\>  
> p tm.definer\_class #-\> Time  
> #---------------------------------
> 
> \<aside:\>  
> 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)  
> \</aside:\>
> 
> daz

---

<div class="post-metadata">

**Author:** ![Pit](https://avatars.discourse-cdn.com/v4/letter/p/f14d63/32.png) [@Pit](https://rubytalk.org/u/Pit)\
**Post date:** [19 July 2005 09:14 UTC](https://rubytalk.org/t/how-to-find-out-a-methods-defining-class/19610/4 "2005-07-19T09:14:41Z")

</div>

Ara.T.Howard \<Ara.T.Howard \<at\> noaa.gov\> writes:

> i don't know a direct way, but maybe:
> 
> &nbsp;&nbsp;&nbsp;harp:~ \> cat a.rb  
> &nbsp;&nbsp;&nbsp;class Object  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;def defines method  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;singleton\_class = class \<\< self; self; end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;([singleton\_class] + self.class.ancestors).reverse.each do |a|  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return a if a.instance\_methods.include? method  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return nil  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;alias defines? defines  
> &nbsp;&nbsp;&nbsp;end

If you want to get at the class reported by Method#inspect, you shouldn't  
reverse the ancestors list. As a consequence, you have to treat singleton  
methods special. If you also want to support private singleton methods, it gets  
really nasty. If someone needs this (support for private singleton methods), I  
can show you the code.

Regards,  
Pit
