Autoload and How-Not-To Require in Ruby 1.9.2.p0|p136
A little known gotcha, the cause of which can be obscure… You can oftentimes see suggested, even when discussing best practices, snippets like the following:
$LOAD_PATH.unshift(File.dirname(__FILE__) + '/../lib') unless \ $LOAD_PATH.include?(File.dirname(__FILE__) + '/../lib')
If you intend to use Ruby’s [autoload], then do not do that. If you do you’ll run into this issue.
Rather, if you have to keep backward compatibility, insert
File.expand_path, or use some of methods from the
Pathname standard library. The Pathname has the advantage of
being more declarative. But you should take care to not feed ‘~’ Pathname,
or just move on to Ruby 1.9.2….
Example (incl. 1.8.7):
require 'pathname' lp = Pathname.new(__FILE__).dirname.realpath.parent + 'lib' $LOAD_PATH.unshift(lp) unless $LOAD_PATH.include?(lp)
If you don’t have to worry about 1.8.7:
Example (1.9.2):
require_relative '../lib/mylibrary'