# Finding files associated with objects in Ruby

**URL:** <https://www.ruby-forum.com/t/finding-files-associated-with-objects-in-ruby/105430>\
**Category:** Ruby\
**Created:** [May 29, 2007, 10:26pm UTC](https://www.ruby-forum.com/t/finding-files-associated-with-objects-in-ruby/105430 "2007-05-29T22:26:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![gparsons](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b487fb/32.png) [@gparsons](https://www.ruby-forum.com/u/gparsons)\
**Post date:** [May 29, 2007, 10:26pm UTC](https://www.ruby-forum.com/t/finding-files-associated-with-objects-in-ruby/105430/1 "2007-05-29T22:26:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gparsons](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b487fb/32.png) [@gparsons](https://www.ruby-forum.com/u/gparsons)\
**Post date:** [May 29, 2007, 10:48pm UTC](https://www.ruby-forum.com/t/finding-files-associated-with-objects-in-ruby/105430/2 "2007-05-29T22:48:43Z")

</div>

There is a Kernel method called “caller” that can help you here. It  
returns  
the current execution stack in the “file:line” format, so if you need to  
know what file a method is define in, you can use the “caller” method to  
show you.

Mark

---

<div class="post-metadata">

**Author:** ![gparsons](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b487fb/32.png) [@gparsons](https://www.ruby-forum.com/u/gparsons)\
**Post date:** [May 30, 2007, 12:11am UTC](https://www.ruby-forum.com/t/finding-files-associated-with-objects-in-ruby/105430/3 "2007-05-30T00:11:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![gparsons](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b487fb/32.png) [@gparsons](https://www.ruby-forum.com/u/gparsons)\
**Post date:** [May 30, 2007, 9:36am UTC](https://www.ruby-forum.com/t/finding-files-associated-with-objects-in-ruby/105430/4 "2007-05-30T09:36:36Z")

</div>

On 29.05.2007 22:22, gparsons wrote:

> 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

---

<div class="post-metadata">

**Author:** ![gparsons](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b487fb/32.png) [@gparsons](https://www.ruby-forum.com/u/gparsons)\
**Post date:** [May 30, 2007, 5:59pm UTC](https://www.ruby-forum.com/t/finding-files-associated-with-objects-in-ruby/105430/5 "2007-05-30T17:59:52Z")

</div>

Daniel L. 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

---

<div class="post-metadata">

**Author:** ![gparsons](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/b487fb/32.png) [@gparsons](https://www.ruby-forum.com/u/gparsons)\
**Post date:** [May 30, 2007, 7:35pm UTC](https://www.ruby-forum.com/t/finding-files-associated-with-objects-in-ruby/105430/6 "2007-05-30T19:35:12Z")

</div>

Daniel DeLorme wrote:

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

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

Dan
