On Oct 18, 2004, at 8:32 PM, Gavin Kistner wrote: > On Oct 18, 2004, at 8:32 AM, trans. (T. Onoma) wrote: >> module Kernel >> # Require files from same dir as running script. >> def import(*args) >> fd = File.dirname(caller[0]) >> args.each do |file_name| >> require File.join(fd, file_name) >> end >> end >> end The following seems to work for me (on MacOS X), tested in the presence of both 'root relative' and root-level paths along the way as part of real 'require' calls. (I've also added this to my 'basiclibrary', documented at http://phrogz.net/RubyLibs/rdoc/classes/Kernel.html) ~/rr_test/start1.rb ~/rr_test/foo/bar/relative_require.rb ~/rr_test/libs/lib1.rb ~/rr_test/libs/lib2.rb ~/rr_test/libs/sublib/lib3.rb ~/rr_test/libs/sublib/lib4.rb % cat start1.rb require 'foo/bar/relative_require.rb' #require 'libs/lib1.rb' require_relative 'libs/lib1.rb' % cat foo/bar/relative_require.rb module Kernel def require_relative( *paths ) path_match = Regexp.new( "^.+#{File::SEPARATOR}" ) paths.each{ |path| file_path = caller[2] && caller[2].match( path_match ) full_path = file_path && file_path[0] || '' puts "require '#{full_path + path}'" if $DEBUG require full_path + path } end end % cat libs/lib1.rb 1.times{ #require '/Users/gavinkistner/rr_test/libs/lib2.rb' require_relative 'lib2.rb' } % cat libs/lib2.rb class Bar class Jim #require 'libs/sublib/lib3.rb' require_relative 'sublib/lib3.rb' end end % cat libs/sublib/lib3.rb #require 'libs/sublib/lib4.rb' require_relative 'lib4.rb' % cat libs/sublib/lib4.rb puts "Congratulations, you made it to lib4!" % ruby -d start1.rb require 'libs/lib1.rb' require './libs/lib2.rb' require './libs/sublib/lib3.rb' require './libs/sublib/lib4.rb' Congratulations, you made it to lib4! % cd libs && ruby -d start2.rb require 'lib1.rb' require './lib2.rb' require './sublib/lib3.rb' require './sublib/lib4.rb' Congratulations, you made it to lib4!