From: Matthias Reitinger Date: 2008-09-02T06:25:50+09:00 Subject: Re: Where the Required Things Are (#175) This is what my solution does: --- ~$ ruby modwhich.rb date set bigdecimal require "date" => /usr/lib/ruby/1.8/date.rb require "set" => /usr/lib/ruby/1.8/set.rb require "bigdecimal" => /usr/lib/ruby/1.8/i486-linux/bigdecimal.so ~$ ruby modwhich.rb /usr/lib/ruby/1.8/date require "/usr/lib/ruby/1.8/date" => /usr/lib/ruby/1.8/date.rb ~$ cat modwhich_test.rb require 'date' require 'set' require 'bigdecimal' ~$ ruby -rmodwhich modwhich_test.rb require "date" => /usr/lib/ruby/1.8/date.rb require "set" => /usr/lib/ruby/1.8/set.rb require "bigdecimal" => /usr/lib/ruby/1.8/i486-linux/bigdecimal.so ~$ MODWHICH_VERBOSE=1 ruby -rmodwhich modwhich_test.rb (modwhich_test.rb:1) require "date" => /usr/lib/ruby/1.8/date.rb (/usr/lib/ruby/1.8/date.rb:196) require "rational" => /usr/lib/ruby/1.8/rational.rb (/usr/lib/ruby/1.8/date.rb:197) require "date/format" => /usr/lib/ruby/1.8/date/format.rb (/usr/lib/ruby/1.8/date/format.rb:4) require "rational" => /usr/lib/ruby/1.8/rational.rb (modwhich_test.rb:2) require "set" => /usr/lib/ruby/1.8/set.rb (modwhich_test.rb:3) require "bigdecimal" => /usr/lib/ruby/1.8/i486-linux/bigdecimal.so --- In code it looks like this: --- require 'pathname' module ModWhich SUFFIXES = [''] + %w{ .rb .rbw .o .so .bundle .dll .sl .jar } def self.which(mod) if Pathname.new(mod).absolute? paths = File.dirname(mod).to_a mod = File.basename(mod) else paths = $LOAD_PATH end file_names = SUFFIXES.map { |suff| mod + suff } paths.each do |path| file_names.each do |file| full_path = File.expand_path(File.join(path, file)) return full_path if File.file?(full_path) end end nil end def self.report(mod) puts "require %s => %s" % [mod.inspect, which(mod) || "(no such file)"] end def self.verbose? ENV['MODWHICH_VERBOSE'] end def self.trap_require Kernel.module_eval do alias :modwhich_original_require :require def require(arg) if ModWhich::verbose? || caller.first.split(':')[0] == $0 if ModWhich::verbose? print "(#{caller.first}) " end ModWhich::report(arg) end modwhich_original_require(arg) end end end end if __FILE__ == $0 while mod = ARGV.shift ModWhich::report(mod) end else ModWhich::trap_require end --- As a pastie: http://pastie.org/264164 I think I'm gonna keep that around, looks useful to me. Regards, Matthias -- Posted via http://www.ruby-forum.com/.