Class/Method Filename

E

evronm

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

Austin Ziegler

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

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

-austin
 
C

Christophe Grandsire

En r=E9ponse =E0 evronm :
=20
Is there any way to know the name of the file in which a class or
method was defined?
=20

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

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

http://rainbow.conlang.free.fr

You need a straight mind to invent a twisted conlang.
 
E

evronm

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

Lyndon Samson

------=_Part_7981_22426588.1129617502045
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
Content-Disposition: inline

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

------=_Part_7981_22426588.1129617502045--
 
R

Ryan Leavengood

Does anyone have any other ideas?

An experiment:

class Class
def inherited(p1)
@@filenames ||=3D {}
@@filenames[p1]=3D(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
 
E

evronm

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

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

Forum statistics

Threads
474,181
Messages
2,570,970
Members
47,536
Latest member
VeldaYoung

Latest Threads

Top