From: Joel VanderWerf Date: 2005-10-22T06:18:16+09:00 Subject: Re: Getting a file location and name... Joe Van Dyk wrote: > Hi, > > Say I have /joe/configuration/config.rb: > ============================== > puts "this configuration file is located in this directory: " + > File.dirname( ????? ) > eval File.read(File.dirname( ?????? ) + "/common.rb") > # other stuff > ============================== > > common.rb is located in /joe/configuration. It also contains some Ruby code. > > > The main program looks something like this: > ============================== > config_file = ARGV.shift > eval File.read config_file > ============================== > > And I'd start the program via 'ruby main.rb /joe/configuration.rb'. > > So, I have a main program that reads in a Ruby configuration file that > can also read other configuration files that are in a relative > directory. I'm having problems with the relative directory part. If > I hardcode the directory paths, everything's ok. But I don't want > /joe/configuration/config.rb to know that it's in /joe/configuration. > I just want that configuration file to be able to read > /joe/configuration/common.rb. > > Sorry if this is confusing, let me know if I can clarify the question. > > Joe > What about something like def with_load_path(path) $LOAD_PATH.unshift path yield ensure $LOAD_PATH.shift end Then use that around your #eval call in the main program, with the '/joe/configuration' as the argument. Then require should work relative to the path you gave. (But note that $LOADED_FEATURES will prevent files with the same name from being loaded again, even if in a different config dir.) Not thread safe, but oh well. Another option: my "script" lib (see RAA). That makes sure that the global $LOADED_FEATURES doesn't get affected. -- vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407