# Is there a way to get the paths to all previously required files?

**URL:** <https://www.ruby-forum.com/t/is-there-a-way-to-get-the-paths-to-all-previously-required-files/156904>\
**Category:** Ruby\
**Created:** [January 5, 2009, 5:40am UTC](https://www.ruby-forum.com/t/is-there-a-way-to-get-the-paths-to-all-previously-required-files/156904 "2009-01-05T05:40:29Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![timr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/3e96dc/32.png) [@timr](https://www.ruby-forum.com/u/timr)\
**Post date:** [January 5, 2009, 5:40am UTC](https://www.ruby-forum.com/t/is-there-a-way-to-get-the-paths-to-all-previously-required-files/156904/1 "2009-01-05T05:40:29Z")

</div>

Let’s say I am in irb and I execute

> > require 'base64.rb"  
> > =\>true

and then I want to know the path to the base64.rb file,  
are the paths to previously required files stored in a global  
variable?

I wan’t to do something like:

> > $required\_files  
> > =\>[’/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/  
> > ruby/1.8/base64.rb’]

Anyone?  
Thanks,  
Tim

P.S. I know that I can do shift+cmd+D in text mate and open the file,  
which obviously requires the path–but this script uses  
TM\_SUPPORT\_PATH–and it seems like something that could be more  
easilly accessed directly from RUBY. At any rate, I would rather be  
able to do it in irb. Thanks.

################From ‘open require’ command of the RUBY bundle of  
TextMate######################  
#!/usr/bin/env ruby

require “#{ENV[‘TM\_SUPPORT\_PATH’]}/lib/ui.rb”  
require “#{ENV[‘TM\_SUPPORT\_PATH’]}/lib/textmate.rb”

REQUIRE\_RE = /^\s\*(?:require|load)\s\*([’"])([^’"#]+?)(?:.rb)?\1[\t]\*  
$/

gems\_installed = begin  
require ‘rubygems’  
true  
rescue LoadError  
false  
end

requires = if ENV[‘TM\_CURRENT\_LINE’].to\_s =~ REQUIRE\_RE  
["#{$2}.rb"]  
else  
$stdin.read.scan(REQUIRE\_RE).map { |\_, path| “#  
{path}.rb” }  
end  
abort ‘No includes found.’ if requires.empty?

file = if requires.size \> 1  
choice = TextMate::UI.menu(requires) or exit  
requires[choice]  
else  
requires.pop  
end  
dir = $LOAD\_PATH.find { |dir| File.exist? File.join(dir, file) }  
if not dir and gems\_installed and gem\_spec =  
Gem::GemPathSearcher.new.find(file)  
dir = File.join(gem\_spec.full\_gem\_path, gem\_spec.require\_path)  
end

if file and dir  
dir.sub!(%r{\A.(?=/|\z)}, ENV[‘TM\_DIRECTORY’]) if ENV  
[‘TM\_DIRECTORY’]  
file\_path = File.join(dir, file)  
puts file\_path  
TextMate.go\_to :file =\> file\_path  
exit  
else  
puts “File not found: #{file}”  
end

---

<div class="post-metadata">

**Author:** ![timr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/3e96dc/32.png) [@timr](https://www.ruby-forum.com/u/timr)\
**Post date:** [January 5, 2009, 6:05am UTC](https://www.ruby-forum.com/t/is-there-a-way-to-get-the-paths-to-all-previously-required-files/156904/2 "2009-01-05T06:05:02Z")

</div>

Look at:  
$" (historic variable)  
$LOADED\_FEATURES (English equivalent)

It’s not exactly what you want, but you could combine those with:  
$:  
$LOAD\_PATH

To find out where each one came from. (Assuming, of course, that the  
LOAD\_PATH hasn’t been altered since the require happened.)

-Rob

Rob B. [http://agileconsultingllc.com](http://agileconsultingllc.com)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![timr](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/3e96dc/32.png) [@timr](https://www.ruby-forum.com/u/timr)\
**Post date:** [January 5, 2009, 6:36am UTC](https://www.ruby-forum.com/t/is-there-a-way-to-get-the-paths-to-all-previously-required-files/156904/3 "2009-01-05T06:36:01Z")

</div>

Here is a one liner that does it. Thanks for the hint.

> > @file = “/mathn.rb”  
> > $:.each{|next\_search\_path| puts next\_search\_path + @file if File::exists?(next\_search\_path + @file)}  
> > /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/  
> > 1.8/mathn.rb
