"George Moschovitis" <george.moschovitis@gmail.com> schrieb im Newsbeitrag
news:1106922976.553171.111240@c13g2000cwb.googlegroups.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.
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,
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:
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!