On May 17, 2006, at 4:14 PM, Logan Capaldo wrote: > > On May 17, 2006, at 4:03 PM, Logan Capaldo wrote: > >> >> On May 17, 2006, at 10:11 AM, Gregory Seidman wrote: >> >>> def find_require_file(filename) >>> # NEED IMPLEMENTATION HERE >>> end >> >> def find_require_file(filename) >> $LOAD_PATH.each do |directory| >> [ ".rb", ".so", ".dll" ].each do |ext| >> if File.exists?(f = File.join(directory, filename + ext)) >> return f >> end >> end >> end >> # otherwise we didn't find it >> raise 'Could not find #{filename} in $LOAD_PATH.' >> end >> >> >> > > Actually in retrospect, I should have used double quotes in the > raise and if you know you're always gonna be eval-ing these files, > you probably want to get rid of the .dll and .so extensions. > > One more thing, this won't work with gems unless you use require_gem. e.g. % irb irb(main):001:0> irb(main):002:0* def find_require_file(filename) irb(main):003:1> $LOAD_PATH.each do |directory| irb(main):004:2* [ ".rb", ".so", ".dll" ].each do |ext| irb(main):005:3* if File.exists?(f = File.join(directory, filename + ext))irb(main):006:4> return f irb(main):007:4> end irb(main):008:3> end irb(main):009:2> end irb(main):010:1> # otherwise we didn't find it irb(main):011:1* raise 'Could not find #{filename} in $LOAD_PATH.' irb(main):012:1> end => nil irb(main):013:0> find_require_file('parse_tree') RuntimeError: Could not find #{filename} in $LOAD_PATH. from (irb):11:in `find_require_file' from (irb):13 irb(main):014:0> require 'rubygems' => true irb(main):015:0> find_require_file('parse_tree') RuntimeError: Could not find #{filename} in $LOAD_PATH. from (irb):11:in `find_require_file' from (irb):15 irb(main):016:0> require_gem 'ParseTree' => true irb(main):017:0> find_require_file('parse_tree') => "/usr/local/ruby/lib/ruby/gems/1.8/gems/ParseTree-1.4.1/lib/ parse_tree.rb" irb(main):018:0> Since of how rubygems alter the load path. If you are using a gem, as long as it doesn't use the deprecated (and evil) autorequire attribute, you should still be able to safely stick it in its own namespace.