From: Zach Dennis Date: 2005-01-29T03:08:59+09:00 Subject: 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