From: dblack@... Date: 2003-01-12T11:33:12+09:00 Subject: Re: 'require' search path Hi -- On Sun, 12 Jan 2003, Tom Sawyer wrote: > On Saturday 11 January 2003 06:38 pm, dblack@candle.superlink.net wrote: > > > please test and improve upon. i have only done so minorly. notice also > > > that it adds my much wished for ability to auto-require things like > > > 'dbi/dbi' just by using 'dbi'. > > > > And seems to do away with the ability to require things like 'scanf' > > just by using 'scanf'.... > > > > candle:~/hacking/ruby$ ruby req.rb > > req.rb:10:in `require': No such file to load -- scanf/scanf (LoadError) > > from req.rb:10:in `require' > > from req.rb:6:in `require' > > from req.rb:18 > > > > In any event, I think it might be best to consider writing a new > > method, rather than overriding require, if you want to create a new > > package-loading functionality. 'require' and 'load' are spoken for, > > and you're going to end up quarantining your code from the rest of > > Ruby if you redefine them. > > David, > > well, hopefully it just needs a fix. i don't seem to have scanf. so i can't > test this. i take it though that the problem is that there is a scanf > directory and a scanf include file. the require is finding the directory > first and thus failing. hmm...just need to make sure it looks for the file > first, prior to trying it as a "directory-require". how to do?... There's a file called 'scanf.rb' in site-ruby/1.6/, no scanf directory. > yes, there may be some unexpected "successful" requires due to this code that > normally would fail, but (once fixed) i see no reason that it should cause > previous code to break. so there should be no issue of quarantining. Well, if your first point is true, then I don't think you can be sure the second point is true :-) Why not just rename the method? There's no need to compete with Ruby for this namespace. > also, that was an aside adjustment. the second part is the more intersesting > of the two alterations. see any problems with that? i esspecially like this. > i find it awfully painful to have to go through my files and change requires > simple b/c i've decided to put them in a different directory. That means that you want to be able to have two file placements which are evaluated identically by 'require'. You want "require 'xxx'" to be able to mean either of two different things. However, problems arise when both of those things exist. Let's say you start with: .../site-ruby/1.6/my_lib/ .../site-ruby/1.6/my_lib/my_lib.rb and you rewrite require so you can do: require 'my_lib' and it loads my_lib/my_lib.rb. What happens now if you add a new file: .../site-ruby/1.6/my_lib.rb This isn't a very likely scenario, but my point is that if you use one expression to represent two things, then you cannot later fully recover the difference between those things. 'require' actually deals with things like this already, in some situations (like file.rb and file.so both existing). But by changing it, you risk introducing incompatible solutions to new problems. At the very least (or the very most), you'd probably want to write a superset of 'require'. If 'require' succeeds, stop. If it fails, apply your new set of extra rules. David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav