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