Forum: Ruby is there a way to get the paths to all previously required files?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
timr (Guest)
on 2009-01-05 06:40
(Received via mailing list)
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
Rob B. (Guest)
on 2009-01-05 07:05
(Received via mailing list)
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
removed_email_address@domain.invalid
timr (Guest)
on 2009-01-05 07:36
(Received via mailing list)
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
This topic is locked and can not be replied to.