Erik Hollensbe wrote:
> On 2006-03-31 10:52:55 -0800, Stephen Tashiro <tashiro / zianet.com> said:
> 
>> 
>> I see, by experiment, that one can maipulate $LOAD_PATH within a Ruby 
>> program.  The second edition of the Pixaxe book indicates that using 
>> rubygems is a good way.
>> 
>> What are some examples of "best practices" for $LOAD_PATH management?
> 
> Rails handles this very well, I've duplicated it in my own projects and
> have had great success.
> 
> What rails does is unshift onto the load path based on the directory of
> the current file, require the files necessary, then shift that item off
> the load path (to keep duplicate filenames from being improperly
> loaded).
> 
> It looks something (although you may want to read the Rails source) like 
> this:
> 
> $:.unshift(File.dirname(__FILE__) + "/whatever")
> require "foo"
> require "bar"
> require "etc"
> $:.shift
> 
> The nice thing about this situation is that you can easily re-require
> whole projects in irb by loading the file that contains this statement,
> and leave your environment effectively clean after you're done.

NO!  DO NOT DO THIS!

Ok, I'll calm down a bit.

But this will not play nicely with RubyGems.  You cannot assume that you 
are the only one manipulating the load path array.  If you blindly shift 
directories off the end of the array, it is quite possible you will 
shift off something added by  someone else (e.g. RubyGems).

If you add a temporary directory to the load path, you should remove it 
explicitly by matching the name.  Something like this would be better:

  load_directory = File.dirname(__FILE__) + "/whatever"
  $:.unshift load_directory
  require 'foo'
  require 'bar'
  require 'etc'
  $:.delete load_directory

I've check the rails source and I didn't see any examples of using shift 
on the load path array.  Are you sure they do this?

--
-- Jim Weirich

-- 
Posted via http://www.ruby-forum.com/.