Class/Method Filename

Hey All,

I'm something of a Nuby, but I have searched extensively before
posting. I'm hope someone here can help me with the following
question:

Is there any way to know the name of the file in which a class or
method was defined?

I'd really appreciate any help you could give me. I'm trying to write
a rudimentary Rails IDE for Vim, and that information would allow me to
do it very elegantly.

Thanks much,

-Mike.

Not really, for a class, since classes are open. Methods can be
determined a little better, but really only when they go boom.

-austin

···

On 10/17/05, evronm <evronm@dtcinc.net> wrote:

Is there any way to know the name of the file in which a class or
method was defined?

--
Austin Ziegler * halostatue@gmail.com
               * Alternate: austin@halostatue.ca

En réponse à evronm :

Is there any way to know the name of the file in which a class or
method was defined?

The problem is that classes in Ruby are open: they can be extended, even at runtime, and their definition can thus be spread on various files (and don't think it's an exceptional case. It's quite common for instance for standard libraries to add one or two methods in core classes). So "the name of the file in which a class was defined" doesn't mean much. For methods, it would be difficult too.

I know there is __FILE__ which contains the name of the file you are currently in, so while you're running a method it points to the filename of the file the method is defined in. But unless the method itself uses __FILE__, you're out of luck...

···

--
Christophe Grandsire.

http://rainbow.conlang.free.fr

You need a straight mind to invent a twisted conlang.

Thanks for the responses, they make a lot of sense. However, I still
have to believe there is an elegant way (more elegant than trying to
hand parse files) to accomplish what I am trying to accomplish, which
is mapping Classes and methods to their code on disk. Doesn't the
debugger need to do this??

I tried a couple of things in irb such as:

def Class.fn
  __FILE__
end

but that just returns "(irb)".

I spent a good hour searching for documentation on the __FILE__
constant, but to no avail.

Does anyone have any other ideas?

Thanks again,

-Mike.

well, you could redefine Kernel require, and keep track of the global class
namespace before and after each call.

An experiment:

class Class
  def inherited(p1)
    @@filenames ||= {}
    @@filenames[p1]=(caller[0]) if caller.size > 0
  end

  def filename
    @@filenames[self]
  end
end

Put this in a file called classfile.rb, and try this:

C:\_Ryan\ruby>ruby -rclassfile -rerb -rostruct -e'p ERB.filename;p
OpenStruct.filename'
"c:/ruby/lib/ruby/1.8/erb.rb:238"
"c:/ruby/lib/ruby/1.8/ostruct.rb:33"

This doesn't work for modules.

Ryan

···

On 10/18/05, evronm <evronm@dtcinc.net> wrote:

Does anyone have any other ideas?

You could use

SCRIPT_LINES__={}; $SAFE=0

to capture all subsequent 'requires' and 'loads', but that doesn't tell
you where core and std. lib methods come from, or work for C module (i
think)

http://redhanded.hobix.com/inspect/whoaScript_lines__.html

Lyndon Samson wrote:

···

well, you could redefine Kernel require, and keep track of the global class
namespace before and after each call.

Thanks for all the help, folks. Great stuff. I'm psyched to know that
what I want to do is possible.