# How do you know what class defines method foo()?

**URL:** <https://www.ruby-forum.com/t/how-do-you-know-what-class-defines-method-foo/50929>\
**Category:** Ruby\
**Created:** [December 22, 2005, 11:00pm UTC](https://www.ruby-forum.com/t/how-do-you-know-what-class-defines-method-foo/50929 "2005-12-22T23:00:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [December 22, 2005, 11:00pm UTC](https://www.ruby-forum.com/t/how-do-you-know-what-class-defines-method-foo/50929/1 "2005-12-22T23:00:23Z")

</div>

Hi, folks!

What’s the easiest(simplest) code to find a class/module that first  
defines a specific method?  
I became curious while I was looking for a document of Array#partition  
method.  
Actually the method is defined in Enumerable module.  
But I couldn’t guess that it was defined there until I checked Array  
class’s doc first.  
If the inheritance(or mixin) hierarchy is very complex, it will take  
long to find a method.

The one I can think of is using MyClass.ancestors and  
MyClass.instance\_methods recursively (or repeatedly).  
Is there a better way?  
(On my second thought, singleton classes should also be considered,  
right?)

The method prototype might be like…

#returns a class or module which defines the method  
def defined\_where(obj, method\_name)  
…  
end

Thanks.

Sam

---

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [December 23, 2005, 12:12am UTC](https://www.ruby-forum.com/t/how-do-you-know-what-class-defines-method-foo/50929/2 "2005-12-23T00:12:46Z")

</div>

On 12/22/05, Sam K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi, folks!
> 
> What’s the easiest(simplest) code to find a class/module that first  
> defines a specific method?

$ ri partition

## --------------------------------------------------- Enumerable#partition enum.partition {| obj | block } =\> [true\_array, false\_array]

```
 Returns two arrays, the first containing the elements of _enum_ for
 which the block evaluates to true, the second containing the rest.

    (1..6).partition {|i| (i&1).zero?} #=> [[2, 4, 6], [1, 3, 5]]

```

---

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [December 23, 2005, 12:15am UTC](https://www.ruby-forum.com/t/how-do-you-know-what-class-defines-method-foo/50929/3 "2005-12-23T00:15:46Z")

</div>

On Dec 22, 2005, at 4:57 PM, Sam K. wrote:

> long to find a method.  
> def defined\_where(obj, method\_name)  
> …  
> end
> 
> Thanks.
> 
> Sam

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 ).

---

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [December 23, 2005, 7:11am UTC](https://www.ruby-forum.com/t/how-do-you-know-what-class-defines-method-foo/50929/4 "2005-12-23T07:11:08Z")

</div>

> 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.

This is exactly what I wanted.  
I didn’t know that there’s ‘method’ method.  
Thank you very much.

Sam

---

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [December 23, 2005, 9:18pm UTC](https://www.ruby-forum.com/t/how-do-you-know-what-class-defines-method-foo/50929/5 "2005-12-23T21:18:57Z")

</div>

Dominik B. wrote:

> 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.

But there ought to be. If you think so as well please vote on this RCR:

[http://www.rcrchive.net/rcr/show/292](http://www.rcrchive.net/rcr/show/292)

---

<div class="post-metadata">

**Author:** ![ssk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/82dd89/32.png) [@ssk](https://www.ruby-forum.com/u/ssk)\
**Post date:** [December 23, 2005, 5:01am UTC](https://www.ruby-forum.com/t/how-do-you-know-what-class-defines-method-foo/50929/6 "2005-12-23T05:01:41Z")

</div>

On Fri, 23 Dec 2005 00:12:51 +0100, Logan C.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 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)  
> > =\> #\<Method: Array(Enumerable)#partition\>  
> > a.method(:first)  
> > =\> #\<Method: Array#first\>  
> > a.method(:send)  
> > =\> #\<Method: Array(Kernel)#send\>  
> > class Array; def send \*a; super end end  
> > =\> nil  
> > a.method(:send)  
> > =\> #\<Method: Array#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
