Finding relative libraries when using symlinks to ruby executables

Posted by dgtized on Stack Overflow See other posts from Stack Overflow or by dgtized
Published on 2008-11-27T02:06:59Z Indexed on 2010/04/20 9:43 UTC
Read the original article Hit count: 182

Filed under:
|
|
|

Imagine you have an executable foo.rb, with libraries bar.rb layed out in the following manner:

<root>/bin/foo.rb
<root>/lib/bar.rb

In the header of foo.rb you place the following require to bring in functionality in bar.rb:

require File.dirname(__FILE__)+"../lib/bar.rb"

This works fine so long as all calls to foo.rb are direct. If you put as say $HOME/project, and symlink foo.rb into $HOME/usr/bin, then __FILE__ resolves to $HOME/usr/bin/foo.rb, and is thus unable to locate bar.rb in relation to the dirname for foo.rb.

I realize that packaging systems such as rubygems fix this by creating a namespace to search for the library, and that it is also possible to adjust the load_path using $: to include $HOME/project/lib, but it seems as if a more simple solution should exist. Has anyone had experience with this problem and found a useful solution or recipe?

© Stack Overflow or respective owner

Related posts about ruby

Related posts about best-practices