From: Jan Svitok Date: 2007-01-04T17:56:34+09:00 Subject: Re: Managing "requires" in projects with many subdirectories On 1/3/07, Eric Hodel wrote: > On Jan 2, 2007, at 15:50, jeffz_2002@yahoo.com wrote: > > > I'd like to know how people people managing their file dependencies > > with projects with several directories. [...] > > > > I've been working on a small project with several directories. Using > > the $:.unshift File.join( ( File.expand_path( File.dirname( __FILE__ ) > > ) ), '..', '..', 'blah' ) trick gets pretty boring to type, doesn't do > > much for the code readability, and doesn't help when files get moved > > around. > > > > require 'find' > > Find.find( File.expand_path( File.dirname( __FILE__ ) ) ) do | > > path| > > if FileTest.directory?(path) > > $:.unshift path > > end > > end > > This'll work great right up until you have something like myproj/ > time.rb and you also need time.rb from stdlib. > > Instead, use explicit paths like everybody else does, it'll be less > confusing. I agree with Eric. We used to add all directories recusively to the search path, and used relative paths without directories. It was nice and all but: - you need to keep the filenames unique. I was bitten by this when suddenly I used a filename of a file from unrelated project, that got on the search path somehow (it was located in the parent directory of my stuff). It took me some time until I realized what's going on. - if you give the code to another person, he/she can't tell from the source where is the required file located. he/she has to figure out by serching the whole tree. - if you mix these two approches, you may and up with some files required more times, which may or may not be good. Now I'm slowly going back to requires with relative paths to 'lib' directory, that gets on the search path from the main file or command line. NB: in the $:.unshift File.join( File.expand_path( File.dirname( __FILE__ )) , '..', '..', 'blah' ) I would reverse expand_path and join, to remove those .. elements, i.e. $:.unshift File.expand_path( File.join( File.dirname( __FILE__ ), '..', '..', 'blah' ))