[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Finding files associated with objects in Ruby

gparsons

5/29/2007 8:22:00 PM

This is a bit of an odd request, but does anyone know how i would find
what file Ruby is using to define an object in? For instance if i had
a few libraries in my include path and they defined the (ever useful)
classes Foo, Bar, and Baz is there anyway to find out from within ruby
that Foo is defined in the file /usr/local/lib/ruby/gems/1.8/gems/
foo-1.0.0/lib/foo.rb or wherever?

The best i could come up with would be a rather brute force approach
to searching through the files found in $: and this just seems
horribly slow and prone to explosions. Just wondering if someone out
there who has a better understanding of Ruby's underbelly might have a
better idea.

/Geoff

4 Answers

Daniel Lucraft

5/29/2007 10:11:00 PM

0

gparsons wrote:
> This is a bit of an odd request, but does anyone know how i would find
> what file Ruby is using to define an object in? For instance if i had
> a few libraries in my include path and they defined the (ever useful)
> classes Foo, Bar, and Baz is there anyway to find out from within ruby
> that Foo is defined in the file /usr/local/lib/ruby/gems/1.8/gems/
> foo-1.0.0/lib/foo.rb or wherever?
>
> The best i could come up with would be a rather brute force approach
> to searching through the files found in $: and this just seems
> horribly slow and prone to explosions. Just wondering if someone out
> there who has a better understanding of Ruby's underbelly might have a
> better idea.
>
> /Geoff

This might not be fully general, but works in the basic case. If
SCRIPT_LINES__ is defined as a hash when a file is loaded, the lines of
the file are stored as an array under the key of the filename.

SCRIPT_LINES__ = {}

require 'foo'

def defining_files(klass)
files = []
SCRIPT_LINES__.each do |filename, lines|
if not lines.grep(/class\s+(\S+::)*#{klass.to_s}/).empty?
files << File.expand_path(filename)
end
end
files
end

defining_files(Foo)

best,
Dan

--
Posted via http://www.ruby-....

Robert Klemme

5/30/2007 7:33:00 AM

0

On 29.05.2007 22:22, gparsons wrote:
> This is a bit of an odd request, but does anyone know how i would find
> what file Ruby is using to define an object in? For instance if i had
> a few libraries in my include path and they defined the (ever useful)
> classes Foo, Bar, and Baz is there anyway to find out from within ruby
> that Foo is defined in the file /usr/local/lib/ruby/gems/1.8/gems/
> foo-1.0.0/lib/foo.rb or wherever?
>
> The best i could come up with would be a rather brute force approach
> to searching through the files found in $: and this just seems
> horribly slow and prone to explosions. Just wondering if someone out
> there who has a better understanding of Ruby's underbelly might have a
> better idea.

Note that the caller approach either requires to manipulate the class
that you are looking for or you need to know that it has a method that
will accept a block which you can use to inject your "locating code".

Given the frequency (or rather /in/frequency) with which this comes up
here it seems that most people are OK with the brute force approach. :-)

Kind regards

robert

Daniel DeLorme

5/30/2007 3:59:00 PM

0

Daniel Lucraft wrote:
> This might not be fully general, but works in the basic case. If
> SCRIPT_LINES__ is defined as a hash when a file is loaded, the lines of
> the file are stored as an array under the key of the filename.
>
> SCRIPT_LINES__ = {}

This is the most obscure and arcane bit of ruby knowledge I have learned
in a loooong time. Bravo

Daniel

Daniel Lucraft

5/30/2007 5:35:00 PM

0

Daniel DeLorme wrote:
> This is the most obscure and arcane bit of ruby knowledge I have learned
> in a loooong time. Bravo

Thank Dave Thomas. I just decided to read Programming Ruby Part Three
from cover to cover yesterday.

Dan

--
Posted via http://www.ruby-....