From: matz@... (Yukihiro Matsumoto) Date: 2001-09-19T01:21:51+09:00 Subject: [ruby-talk:21340] Re: nested require files need path internally Hi, In message "[ruby-talk:21331] Re: nested require files need path internally" on 01/09/19, Paul Brannan writes: |The problem with doing this is that if I have a directory structure like |this: | | | | +- foo | | +- foo.rb | | +- bar.rb | | | +- mystuff.rb | |And in mystuff.rb I have: | | require './foo/foo.rb' | |And in foo.rb I have: | | require './bar.rb' | |Then the require will fail, because bar.rb is not in the current |directory. I'm not sure what is your point. If you really want to read from the current directory hierarchy (which is pretty unreliable), you can prepend "." to $LOAD_PATH variable. |Additionally, part of the problem that was brought up was that if I do: | | require 'foo.rb' | require 'foo' | |both of these will cause the same file to be required; the file will, in |fact, be loaded twice. This is evident by the following simple program: | | if not defined?(x) then x = '' end | x << './' | require "#{x}#{__FILE__}" | |This will load the SAME FILE over 100 times. Wait a second. | require 'foo.rb' | require 'foo' does not load twice. In your example, you loaded a file using different path. Normalizing loaded path is a good thing to see, but I don't think it's mandatory. |Ideally, require would do the following: | | 1) Check for the ./ prefix; if it is there, look in the same directory | as __FILE__. I don't agree with this. This changes "." semantics. | 2) Check to see if the file is a symlink; if it is, get the name of the | real file being required. Repeat as necessary. | 3) Find the full pathname of the file being required before marking the | file as already having been required. I'm not sure above 2) and 3) are worthy enough, considering loading from current working direcotry is not recommended. matz.