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

Discussion in 'Ruby' started by George Moschovitis, Jan 28, 2005.

  1. 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
    George Moschovitis, Jan 28, 2005
    #1
    1. Advertising

  2. George  Moschovitis

    Pit Capitain Guest

    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
    Pit Capitain, Jan 28, 2005
    #2
    1. Advertising

  3. Re: : How can I find the filename where a class is defined?

    > 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.
    George Moschovitis, Jan 28, 2005
    #3
  4. Re: : How can I find the filename where a class is defined?

    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
    Michael Neumann, Jan 28, 2005
    #4
  5. Re: : How can I find the filename where a class is defined?

    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
    Alexander Kellett, Jan 28, 2005
    #5
  6. Re: : How can I find the filename where a class is defined?

    > 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
    George Moschovitis, Jan 28, 2005
    #6
  7. Re: : How can I find the filename where a class is defined?

    "George Moschovitis" <> schrieb im Newsbeitrag
    news:...
    > > 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
    Robert Klemme, Jan 28, 2005
    #7
  8. George  Moschovitis

    Pit Capitain Guest

    Re: : How can I find the filename where a class is defined?

    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
    Pit Capitain, Jan 28, 2005
    #8
  9. George  Moschovitis

    Sea&Gull Guest

    Re: : How can I find the filename where a class is defined?

    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
    Sea&Gull, Jan 28, 2005
    #9
  10. George  Moschovitis

    Zach Dennis Guest

    Re: : How can I find the filename where a class is defined?

    >>>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
    Zach Dennis, Jan 28, 2005
    #10
  11. George  Moschovitis

    Zach Dennis Guest

    Re: : How can I find the filename where a class is defined?

    Zach Dennis wrote:
    >>>> 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
    >


    If you need it to gooo real fast, we can write a c extension =) and add
    more functions so we don't get charged with being wasteful... The usage
    could be similar to:

    RubySourceParser::find_method_decl( "mymethod" )
    =>[[Class1,"file.rb", lineno=1044], [Class2, "file2.rb", lineno=56]]

    RubySourceParser::find_method_decl( "Class1", "mymethod" ) => ...etc...

    RubySourceParser::find_class_decl( "Class1" ) => ["file1.rb",lineno=10]

    RubySourceParser::find_module_decl( "...etc..." )

    Perhaps this is overkill, but I am on a ruby high right now, after I
    wrote a mini-library to handle NetworkDrives in Windows. It is a small
    very simple thing, but it makes me sooo happy. Happying Rubying!

    Zach
    Zach Dennis, Jan 28, 2005
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Sm9l?=

    Extract filename from a filename typed by user

    =?Utf-8?B?Sm9l?=, Aug 23, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    1,002
    Travis Murray
    Aug 24, 2004
  2. Replies:
    1
    Views:
    1,431
    Roland de Ruiter
    Jun 15, 2006
  3. Ed
    Replies:
    10
    Views:
    45,727
    alok000707
    Jul 13, 2010
  4. Oodini
    Replies:
    1
    Views:
    1,750
    Keith Thompson
    Sep 27, 2005
  5. Beauregard T. Shagnasty

    Re: filename.gif or filename.gif.jpg?

    Beauregard T. Shagnasty, May 30, 2008, in forum: HTML
    Replies:
    1
    Views:
    733
    Jonathan N. Little
    May 30, 2008
Loading...

Share This Page