# Finding the location of libraries

**URL:** <https://rubytalk.org/t/finding-the-location-of-libraries/16187>\
**Category:** ruby-talk\
**Created:** [1 February 2005 17:00 UTC](https://rubytalk.org/t/finding-the-location-of-libraries/16187 "2005-02-01T17:00:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Claus\_Spitzer](https://avatars.discourse-cdn.com/v4/letter/c/49beb7/32.png) [@Claus\_Spitzer](https://rubytalk.org/u/Claus_Spitzer)\
**Post date:** [1 February 2005 17:00 UTC](https://rubytalk.org/t/finding-the-location-of-libraries/16187/1 "2005-02-01T17:00:37Z")

</div>

Greetings fellow rubyists!  
I am looking for a way of checking which location a library I use is  
at. Let me demonstrate with an example:

Let's assume that I have a hypotetical programa program, foo.rb . That  
program requires a library, as in  
&nbsp;&nbsp;&nbsp;require 'bar'

The problem is, I have 4 different versions of bar.rb and one [bar.so](http://bar.so),  
and would like to know which one I am using. How would I go around  
figuring this out?

Cheers!

&nbsp;&nbsp;-C.W.S.

---

<div class="post-metadata">

**Author:** ![Zach\_Dennis1](https://avatars.discourse-cdn.com/v4/letter/z/ea5d25/32.png) [@Zach\_Dennis1](https://rubytalk.org/u/Zach_Dennis1)\
**Post date:** [1 February 2005 17:07 UTC](https://rubytalk.org/t/finding-the-location-of-libraries/16187/2 "2005-02-01T17:07:36Z")

</div>

Claus Spitzer wrote:

> Greetings fellow rubyists! I am looking for a way of checking which location a library I use is  
> at. Let me demonstrate with an example:
> 
> Let's assume that I have a hypotetical programa program, foo.rb . That  
> program requires a library, as in  
> &nbsp;&nbsp;&nbsp;require 'bar'
> 
> The problem is, I have 4 different versions of bar.rb and one bar.so,  
> and would like to know which one I am using. How would I go around  
> figuring this out?

If you know what you are looking for you can search the $: global variable which houses the paths that ruby looks in for files.

require "time"  
$:.each do |path|  
&nbsp;&nbsp;&nbsp;&nbsp;puts "#{path}/time.rb" if File.exist?( "#{path}/time.rb" )  
end

here i cheated because I knew I was looking for time. A better solution is:

def require( what )  
&nbsp;&nbsp;&nbsp;&nbsp;super  
&nbsp;&nbsp;&nbsp;&nbsp;$:.each do |path|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "#{path}/#{what}.rb" if File.exist?( "#{path}/time.rb" )  
&nbsp;&nbsp;&nbsp;&nbsp;end  
end

require "time"

but this will give you output for every required file. Example when I required "time" I get the following output:

---------- Capture Output ----------  
\> "c:\ruby\bin\ruby.exe" loadtest.rb -d  
c:/ruby/lib/ruby/1.8/rational.rb  
c:/ruby/lib/ruby/1.8/date/format.rb  
c:/ruby/lib/ruby/1.8/parsedate.rb  
c:/ruby/lib/ruby/1.8/time.rb

HTH,

Zach

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [1 February 2005 17:20 UTC](https://rubytalk.org/t/finding-the-location-of-libraries/16187/3 "2005-02-01T17:20:48Z")

</div>

"Claus Spitzer" \<docboobenstein@gmail.com\> schrieb im Newsbeitrag  
news:bb13341905020109007036e0d3@mail.gmail.com...

> Greetings fellow rubyists!  
> I am looking for a way of checking which location a library I use is  
> at. Let me demonstrate with an example:
> 
> Let's assume that I have a hypotetical programa program, foo.rb . That  
> program requires a library, as in  
> &nbsp;&nbsp;&nbsp;require 'bar'
> 
> The problem is, I have 4 different versions of bar.rb and one bar.so,  
> and would like to know which one I am using. How would I go around  
> figuring this out?

ruby -e '$LOAD\_PATH.each {|dir| p Dir[File.join(dir,"bar.\*")]}'  
ruby -e 'puts $LOAD\_PATH.select {|dir| !  
Dir[File.join(dir,"bar.\*")].empty?}'

Kind regards

&nbsp;&nbsp;&nbsp;&nbsp;robert

---

<div class="post-metadata">

**Author:** ![Zach\_Dennis1](https://avatars.discourse-cdn.com/v4/letter/z/ea5d25/32.png) [@Zach\_Dennis1](https://rubytalk.org/u/Zach_Dennis1)\
**Post date:** [1 February 2005 17:15 UTC](https://rubytalk.org/t/finding-the-location-of-libraries/16187/4 "2005-02-01T17:15:23Z")

</div>

I had a typo in my last post, the second code fragment should be:

def require( what )  
&nbsp;&nbsp;&nbsp;&nbsp;super  
&nbsp;&nbsp;&nbsp;&nbsp;$:.each do |path|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts "#{path}/#{what}.rb" if File.exist?( "#{path}/#{what}.rb" )  
&nbsp;&nbsp;&nbsp;&nbsp;end  
end

require "time"

HTH,  
Zach

---

<div class="post-metadata">

**Author:** ![Claus\_Spitzer](https://avatars.discourse-cdn.com/v4/letter/c/49beb7/32.png) [@Claus\_Spitzer](https://rubytalk.org/u/Claus_Spitzer)\
**Post date:** [3 February 2005 20:34 UTC](https://rubytalk.org/t/finding-the-location-of-libraries/16187/5 "2005-02-03T20:34:59Z")

</div>

Awesome! Thanks to both you and Zach.

> **···**
>
> On Wed, 2 Feb 2005 02:20:48 +0900, Robert Klemme \<bob.news@gmx.net\> wrote:
> 
> > "Claus Spitzer" \<docboobenstein@gmail.com\> schrieb im Newsbeitrag  
> > news:bb13341905020109007036e0d3@mail.gmail.com...  
> > \> Greetings fellow rubyists!  
> > \> I am looking for a way of checking which location a library I use is  
> > \> at. Let me demonstrate with an example:  
> > \>  
> > \> Let's assume that I have a hypotetical programa program, foo.rb . That  
> > \> program requires a library, as in  
> > \> require 'bar'  
> > \>  
> > \> The problem is, I have 4 different versions of bar.rb and one bar.so,  
> > \> and would like to know which one I am using. How would I go around  
> > \> figuring this out?
> > 
> > ruby -e '$LOAD\_PATH.each {|dir| p Dir[File.join(dir,"bar.\*")]}'  
> > ruby -e 'puts $LOAD\_PATH.select {|dir| !  
> > Dir[File.join(dir,"bar.\*")].empty?}'
> > 
> > Kind regards
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;robert
