# How can I ask to which module a method belongs to?

**URL:** <https://www.ruby-forum.com/t/how-can-i-ask-to-which-module-a-method-belongs-to/67197>\
**Category:** Ruby\
**Created:** [September 1, 2006, 11:41am UTC](https://www.ruby-forum.com/t/how-can-i-ask-to-which-module-a-method-belongs-to/67197 "2006-09-01T11:41:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dk3qu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d2c977/32.png) [@dk3qu](https://www.ruby-forum.com/u/dk3qu)\
**Post date:** [September 1, 2006, 11:41am UTC](https://www.ruby-forum.com/t/how-can-i-ask-to-which-module-a-method-belongs-to/67197/1 "2006-09-01T11:41:38Z")

</div>

Hi List,

more questions from a Ruby-Newbie…

Sam R. from Ruby-Core answered to my original question:

> irb(main):001:0\> s = “hi”  
> Cheers,  
> Sam

That is good news to me. But now I wonder, if and how I can ask e.g.  
String#this(opt) in which module it is defined.  
Any ideas?

Cheers,

Markus

---

<div class="post-metadata">

**Author:** ![dk3qu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d2c977/32.png) [@dk3qu](https://www.ruby-forum.com/u/dk3qu)\
**Post date:** [September 1, 2006, 11:39pm UTC](https://www.ruby-forum.com/t/how-can-i-ask-to-which-module-a-method-belongs-to/67197/2 "2006-09-01T23:39:43Z")

</div>

On 9/1/06, Markus G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > This is probably best posted to ruby-talk, not ruby-core, folks would  
> > =\> nil  
> > irb(main):004:0\> “bye”.this(" that")  
> > bye that  
> > =\> nil
> > 
> > Cheers,  
> > Sam
> 
> That is good news to me. But now I wonder, if and how I can ask e.g.  
> String#this(opt) in which module it is defined.

First note, that a method can be defined in a module or a class, and  
that a class is a kind of module.

I haven’t tested this extensively but I think it works. It searches  
for a method name in a class the same way that methods are found at  
runtime, at each level of the class hierarchy, the class is searched,  
then any modules that class includes in the reverse order they were  
included. This code depends on Module#included\_modules giving it’s  
result in the right order, which a few quick tests seems to indicate:  
module A  
end  
module B  
end  
module C  
include B  
include A  
end

C.included\_modules =\> [A, B]  
module D  
include A  
include B  
end  
D.included\_modules =\> [B, A]

Okay so here’s my code:

class Module  
def module\_defining(method\_name)  
method\_name = method\_name.to\_s  
return self if  
instance\_methods(false).include?(method\_name)  
included\_modules.each do |mixin|  
answer = mixin.module\_defining(method\_name)  
return answer if answer  
end  
nil  
end  
end

class Class  
def module\_defining(method\_name)  
answer = super  
return answer if answer  
return superclass.module\_defining(method\_name) if  
superclass  
end  
end

irb(main):001:0\> load ‘moddefining.rb’  
=\> true  
irb(main):002:0\> String.module\_defining(‘==’)  
=\> String  
irb(main):003:0\> String.module\_defining(‘id’)  
=\> Kernel  
irb(main):004:0\> String.module\_defining(‘map’)  
=\> Enumerable

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![dk3qu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d2c977/32.png) [@dk3qu](https://www.ruby-forum.com/u/dk3qu)\
**Post date:** [September 1, 2006, 11:59pm UTC](https://www.ruby-forum.com/t/how-can-i-ask-to-which-module-a-method-belongs-to/67197/3 "2006-09-01T23:59:44Z")

</div>

> irb(main):001:0\> load ‘moddefining.rb’  
> =\> true  
> irb(main):002:0\> String.module\_defining(’==’)  
> =\> String  
> irb(main):003:0\> String.module\_defining(‘id’)  
> =\> Kernel  
> irb(main):004:0\> String.module\_defining(‘map’)  
> =\> Enumerable

Sexy! 🙂

IMO something like this would make a nice addition to the core library.  
Jumping through hoops for introspection is no fun. (At least this one  
doesn’t involve any string parsing.)

Note that the Method#inspect method gives you this same information in  
string form:

irb(main):001:0\> “”.method( :== )  
=\> #\<Method: String#==\>  
irb(main):002:0\> String.instance\_method( :== )  
=\> #\<UnboundMethod: String#==\>  
irb(main):003:0\> “”.method( :id )  
=\> #\<Method: String(Kernel)#id\>  
irb(main):004:0\> String.instance\_method( :id )  
=\> #\<UnboundMethod: String(Kernel)#id\>  
irb(main):005:0\> “”.method( :map )  
=\> #\<Method: String(Enumerable)#map\>  
irb(main):006:0\> String.instance\_method( :map )  
=\> #\<UnboundMethod: String(Enumerable)#map\>

Less or more reliable to use this with string parsing? Perhaps slightly  
more, if only to be sure that the method resolution order is properly  
followed.

---

<div class="post-metadata">

**Author:** ![dk3qu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d2c977/32.png) [@dk3qu](https://www.ruby-forum.com/u/dk3qu)\
**Post date:** [September 2, 2006, 11:05pm UTC](https://www.ruby-forum.com/t/how-can-i-ask-to-which-module-a-method-belongs-to/67197/4 "2006-09-02T23:05:11Z")

</div>

On 9/1/06, Phrogz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> =\> #\<UnboundMethod: String#==\>  
> more, if only to be sure that the method resolution order is properly  
> followed.

Well although the ri documentation for Module#included\_modules doesn’t  
say wo in so many words, it does in fact produce the list in the right  
order.

The way an included module is implemented is that a ‘hidden’ proxy  
class is inserted between the class which includes the module, and  
it’s superclass. It’s hidden because it’s marked as being a T\_ICLASS.

The included\_modules C code just walks the chain from the super class  
and the superclass of the class, listing any T\_ICLASSes that it finds.

## Since this is the way that method lookup works, it’s exactly the right order.

Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

IPMS/USA Region 12 Coordinator  
[http://ipmsr12.denhaven2.com/](http://ipmsr12.denhaven2.com/)

Visit the Project Mercury Wiki Site  
[http://www.mercuryspacecraft.com/](http://www.mercuryspacecraft.com/)
