Some Ruby librararies I'm using use require
statements like this:
require File.dirname(__FILE__) + '/specification_helper.rb'
lib_dir = File.expand_path(File.join(File.dirname(__FILE__), "lib"))
require File.join(File.dirname(__FILE__), 'lib/tools', 'version')
require File.expand_path(File.join(File.dirname(__FILE__), 'datautils', 'conn'))
Doesn't this format make your code needlessly dependent on the structure of the file system?
Why did the author do it this way?
Would it be possible to (safely) alter this code to remove this dependency on the filesystem?
I actually use this scheme in production code.
Requiring files relative to the current source location has several advantages :
Should you prefer to use a modified ruby search path, you can do it in multiple ways :
Solution 1 implies controlling how ruby is invoked. You'll need a script to start the program, such as:
Or:
Solution 2 is workable but does not avoid collisions. You'll typically do something like :
Solution 3 is unadvisable, the less dependencies you'll have toward environment variables, the better you will be.