I actually use this scheme in production code.
Requiring files relative to the current source location has several advantages :
- the source tree can be moved around as a whole and remains usable since we require sources relatively to each other.
- since we use full paths, we avoid accidental collisions (loading a source with the same name in another library, or reloading the same source twice)
- the code can be used without having to modify ruby's search path
Should you prefer to use a modified ruby search path, you can do it in multiple ways :
- adding -I options on the ruby command line
- modifying the $LOAD_PATH variable within the sources
- playing with the environment variable RUBYLIB
Solution 1 implies controlling how ruby is invoked. You'll need a script to start the program, such as:
@echo off
REM my_script.cmd
set srcdir=%~dp0\..\path\to\source
ruby -I %srcdir% %srcdir%\my_script.rb
Or:
#!/bin/sh
srcdir=$(cd $(dirname $0)/../path/to/source && pwd)
exec ruby -I $srcdir $srcdir/my_script.rb
Solution 2 is workable but does not avoid collisions. You'll typically do something like :
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__)))
Solution 3 is unadvisable, the less dependencies you'll have toward environment variables, the better you will be.