# Show defining file for methods?

**URL:** <https://www.ruby-forum.com/t/show-defining-file-for-methods/112484>\
**Category:** Ruby\
**Created:** [August 20, 2007, 2:30am UTC](https://www.ruby-forum.com/t/show-defining-file-for-methods/112484 "2007-08-20T02:30:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mario\_TSL](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/df705f/32.png) [@Mario\_TSL](https://www.ruby-forum.com/u/Mario_TSL)\
**Post date:** [August 20, 2007, 2:30am UTC](https://www.ruby-forum.com/t/show-defining-file-for-methods/112484/1 "2007-08-20T02:30:49Z")

</div>

I don’t think this is built in, but maybe I’m wrong. I was wondering  
how, if this isn’t built in, we might add the ability when pretty  
printing a class/modules list of methods we might have the name of the  
file where the method was defined.

That is instead of:

pp ActiveRecord.methods.sort  
…  
“tainted?”,  
“to\_a”,  
“to\_json”,  
…

We’d get something like:

pp ActiveRecord.methods\_with\_origination\_info.sort  
…  
“tainted?” (/usr/lib/…),  
“to\_a” (/some/path/to/this/file/…),  
“to\_json” (/path/info/…),  
…

Even more useful if it could somehow tell me that it was included via a  
mixin or via inheritance, etc.:

…  
“to\_a” (/some/path/to/this/file/…) mixed in from Array,  
“bogus\_method” (/some/path/to/this/file/…) inherited from Bogus,  
…

Obviously, this is bogus sample info., however, it makes my point.

This kind of information would make it easier to know where to go to  
look up documentation, especially during debugging/programming. Right  
now when listing all methods for an object or class, the list returned  
is overwhelming. I can’t easily tell what methods are native and which  
ones got plastered on during a very dynamic load and from where they  
came. Or perhaps some other API is more desirable:

#native\_methods  
#mixed\_in\_methods  
#inherited\_methods

It would be useful to gather the same information about instance  
variables, though this is a lesser concern.

I suspect this could be handled with either callbacks or aspect-oriented  
programming. I would also suppose this could be done by using  
“included\_modules” or “ancestors” and accessing their methods to  
appropriately tag who owns what; however, I’m thinking this won’t be  
exact as it should be possible during multiple mixins for a method to be  
overridden more than once. This would through into question ownership,  
that is unless the “included\_modules” or “ancestors” returns the order  
of mixin. Also, this doesn’t handle inheritance.

Having this sort of information would be vastly helpful. The main  
drawback of Ruby is the lack of a power IDE that makes available just  
this sort of information. I know there are some that have come a long  
way (NetBeans??) but right now I basically use gedit and a terminal  
console.

Any thoughts or guidance? Something I’ve overlooked?

Thanks for any advice.  
Mario T. Lanza

---

<div class="post-metadata">

**Author:** ![Mario\_TSL](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/df705f/32.png) [@Mario\_TSL](https://www.ruby-forum.com/u/Mario_TSL)\
**Post date:** [August 20, 2007, 3:54pm UTC](https://www.ruby-forum.com/t/show-defining-file-for-methods/112484/2 "2007-08-20T15:54:14Z")

</div>

On Mon, Aug 20, 2007 at 09:30:50AM +0900, Mario T. Lanza wrote:

> I don’t think this is built in, but maybe I’m wrong. I was wondering  
> how, if this isn’t built in, we might add the ability when pretty  
> printing a class/modules list of methods we might have the name of the  
> file where the method was defined.  
> […]  
> Any thoughts or guidance? Something I’ve overlooked?

With a certain amount of work and hooking of Module::method\_added,  
Module::included, and Class::inherited you could probably get the  
information you want. The base system, however, does not track that  
information. Your hooks would have to gather the info and make it  
available  
through some API, such as what you suggested. Also, if some piece of  
code  
was a poor citizen and overrode one or more of your hooks without making  
sure to call it (either with an alias chain or with super), you’re out  
of  
luck.

> Thanks for any advice.  
> Mario T. Lanza  
> –Greg

---

<div class="post-metadata">

**Author:** ![Mario\_TSL](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/df705f/32.png) [@Mario\_TSL](https://www.ruby-forum.com/u/Mario_TSL)\
**Post date:** [August 20, 2007, 6:43pm UTC](https://www.ruby-forum.com/t/show-defining-file-for-methods/112484/3 "2007-08-20T18:43:29Z")

</div>

When I have a little more time I’ll look into doing what you suggest.  
Thank you for pointing me in the right direction. 🙂
