From: Paul Brannan Date: 2001-12-11T01:11:15+09:00 Subject: [ruby-talk:28095] Re: "load" without looking in $LOAD_PATH ? On Sat, Dec 08, 2001 at 08:46:25PM +0900, Johan Holmberg wrote: > I have also considered makeing the filename argument "absolute" > before passing it to "load". The best way I have found until now is > to call "load" like this: > > load File.expand_path($filename) I don't think that there is a "right" way. See [ruby-talk:21406] and [ruby-talk:20198] and the discussion of "requirelocal": def requirelocal(file, *args) caller_file, *foo = caller[0].split(':') caller_dir = File.dirname(caller_file) require File.join(caller_dir, file) end It should be simple to modify this for "load" instead of "require". > As far as I can see this effectively disables the path-searching > normally done by "load". Is this the right way to do this ??? > Or have I missed some better way ??? The only problem with using this for "require" is that it can load a file twice if you aren't careful. I've taken quite some care to modify the above method to get it to work right in (almost?) all cases (two symlinks to the same file, the user specifying oddly-formed relative paths, etc.): def requirelocal(file) caller_file, caller_lineno, caller_method = parse_caller(caller[0]) caller_dir = File.dirname(caller_file_pathname(caller_file)) require File.join(caller_dir, file) end def loadlocal(file) caller_file, caller_lineno, caller_method = parse_caller(caller[0]) caller_dir = File.dirname(caller_file_pathname(caller_file)) load File.join(caller_dir, file) end verbose = $VERBOSE $VERBOSE = false $__old_require__ = method(:require) $".each do |required_file| file = find_file_in_path(required_file) required_file.sub!(/^.*$/, file) end def require(file) file_to_require = find_file_in_path(file) $__old_require__.call(realpath(file_to_require)) end $__old_load__ = method(:load) def load(file) file_to_load = find_file_in_path(file) $__old_load__.call(file_to_load) end $VERBOSE = verbose $initial_directory = Dir.getwd def caller_file_pathname(filename) File.expand_path(filename, $initial_directory) end def parse_caller(caller_str) file, lineno, method = caller_str.split(':') if method =~ /in `(.*)'/ then method = $1.intern() end return file, lineno.to_i(), method end def realpath(file) if not File.respond_to?(:readlink) then return file end total = '' File.expand_path(file).split(File::SEPARATOR).each do |piece| next if piece == "" if File.symlink?(total + File::SEPARATOR + piece) then total += File::SEPARATOR + piece begin total = File.expand_path( File.readlink(total), File.dirname(total)) end while File.symlink?(total) else total << File::SEPARATOR + piece end end return total end def find_file_in_path(file, path=$:, extensions=['', '.so', '.rb']) path.each do |dir| pathname = File.join(dir, file) extensions.each do |ext| filename = pathname + ext if File.exists?(filename) then return File.expand_path(filename) end end end return file end Paul