Show defining file for methods?

M

Mario T. Lanza

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
 
G

Gregory Seidman

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
 
M

Mario T. Lanza

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top