Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > [Q]: How can I find the filename where a class is defined?

Reply
Thread Tools

[Q]: How can I find the filename where a class is defined?

 
 
George Moschovitis
Guest
Posts: n/a
 
      01-28-2005
Hello everyone,

I would like to find the filename where a given class is defined.
One simple solution is the following:

class MyClass
SOURCE_FILENAME = __FILE__
....
end

then the filename is:

MyClass::SOURCE_FILENAME

This is inelegant though, is there a better solution?
Thanks in advance,
George

 
Reply With Quote
 
 
 
 
Pit Capitain
Guest
Posts: n/a
 
      01-28-2005
Hi George!

> I would like to find the filename where a given class is defined.


Since a class can be spread over multiple files, you have to define which one(s)
you want to get.

> class MyClass
> SOURCE_FILENAME = __FILE__
> ...
> end
>
> This is inelegant though, is there a better solution?


I cannot think of something simpler / better / more elegant...

Regards,
Pit


 
Reply With Quote
 
 
 
 
George Moschovitis
Guest
Posts: n/a
 
      01-28-2005
> Since a class can be spread over multiple files, you have to define
which
> one(s) you want to get.


My class is defined in one file only. I dont want a general solution. I
want a solution for classes defined in a single file. Any other ideas?
-g.

 
Reply With Quote
 
Michael Neumann
Guest
Posts: n/a
 
      01-28-2005
George Moschovitis wrote:
>>Since a class can be spread over multiple files, you have to define

>
> which
>
>>one(s) you want to get.

>
>
> My class is defined in one file only. I dont want a general solution. I
> want a solution for classes defined in a single file. Any other ideas?


Yes, that's what I needed last night, too (and some month ago where I
considered Wee+templating).

class Object
class << self
attr_accessor :classdef_in_file

alias __old_inherited inherited
def inherited(klass)
klass.classdef_in_file = caller.last.split(":").first
__old_inherited(klass)
end
end
end


# in file test.rb

class MyClass; end
p MyClass.classdef_in_file # => test.rb


Regards,

Michael



 
Reply With Quote
 
Alexander Kellett
Guest
Posts: n/a
 
      01-28-2005
On Jan 28, 2005, at 3:40 PM, George Moschovitis wrote:
>> Since a class can be spread over multiple files, you have to define

> which
>> one(s) you want to get.

>
> My class is defined in one file only. I dont want a general solution. I
> want a solution for classes defined in a single file. Any other ideas


t.rb
class Class
def inherited t
puts "class #{t} created at #{caller.first}"
end
end
require 'g.rb'

g.rb:
class Blah
end

->

class Blah created at ./g.rb:1



 
Reply With Quote
 
George Moschovitis
Guest
Posts: n/a
 
      01-28-2005
> Yes, that's what I needed last night, too (and some month ago where I
> ...
> class MyClass; end
> p MyClass.classdef_in_file # => test.rb


Thank you very much!

> considered Wee+templating).


I want this for a similar reason (new version of controller/action
reloading in Nitro)

-g.

--
http://www.rubyforge.com/projects/nitro

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      01-28-2005

"George Moschovitis" <(E-Mail Removed)> schrieb im Newsbeitrag
news:(E-Mail Removed) oups.com...
> > Since a class can be spread over multiple files, you have to define

> which
> > one(s) you want to get.

>
> My class is defined in one file only. I dont want a general solution. I
> want a solution for classes defined in a single file. Any other ideas?
> -g.


Maybe you can cook something up with set_trace_func. So you can set the
trace funtion at the beginning of your script and fill some global mapping
from class to file name.

robert

 
Reply With Quote
 
Pit Capitain
Guest
Posts: n/a
 
      01-28-2005
Michael Neumann schrieb:
>
> class Object
> class << self
> attr_accessor :classdef_in_file
>
> alias __old_inherited inherited
> def inherited(klass)
> klass.classdef_in_file = caller.last.split(":").first
> __old_inherited(klass)
> end
> end
> end


This is a nice idea!

Note that it doesn't work on Windows, though, cause there you have filenames
with colons ("C:/xxx"). You could change the line

> klass.classdef_in_file = caller.last.split(":").first


to

> klass.classdef_in_file = caller.last.sub(/:\d+$/, "")



BTW: now I can finally get rid of all those ugly

if $0 == __FILE__
end

Code

Thanks for the reminder,
Pit


 
Reply With Quote
 
Sea&Gull
Guest
Posts: n/a
 
      01-28-2005
Michael Neumann wrote:
> George Moschovitis wrote:
>
>>> Since a class can be spread over multiple files, you have to define

>>
>>
>> which
>>
>>> one(s) you want to get.

>>
>>
>>
>> My class is defined in one file only. I dont want a general solution. I
>> want a solution for classes defined in a single file. Any other ideas?

>
>
> Yes, that's what I needed last night, too (and some month ago where I
> considered Wee+templating).
>
> class Object
> class << self
> attr_accessor :classdef_in_file
>
> alias __old_inherited inherited
> def inherited(klass)
> klass.classdef_in_file = caller.last.split(":").first
> __old_inherited(klass)
> end
> end
> end


Don't you forget

private :inherited

?



Also I would like to ask why you used "class Object" but not "class Class"?
There are some reasons I haven't seen?

--
s&g
 
Reply With Quote
 
Zach Dennis
Guest
Posts: n/a
 
      01-28-2005
>>>George Moschovitis wrote:

>>> My class is defined in one file only. I dont want a general solution. I
>>> want a solution for classes defined in a single file. Any other ideas?


I dont' know what kind of performance you are looking for, but if you
know the file:

classes = {}
File.open( "file.rb" ) do |file|
file.each_line do |line|
next unless line =~ /\s*class\s*(\w+)/
classes[ $1 ] = file.lineno
end
end

puts "I found the class MyClass in file file.rb at line
#{classes['MyClass']}"

This will build a hash of your class to lineno found in the source
"file.rb". the "file.rb" could also be replaced with just saying
__FILE__ if you are wanting to parse the currently loaded file.

This could easily be expanded to suit any need, but granted it reparses
your source files and thus is slower and less efficient,

Zach


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Re: Is it possible to let a virtual file created by cStringIO havea filename so that functions can read it by its filename? Steven Howe Python 0 01-14-2011 10:32 PM
Stitch rar files ( filename.part01 and filename.part02) Please help ixgor Software 1 10-15-2006 02:33 AM
how to get 8.3 format filename from long filename jacobyv@sis.unibe.ch Java 1 06-15-2006 10:39 AM
Extract filename from a filename typed by user =?Utf-8?B?Sm9l?= ASP .Net 1 08-23-2004 11:29 PM



Advertisments