From: Aaron Patterson Date: 2009-08-26T00:59:31+09:00 Subject: Re: Packaging - distribution On Tue, Aug 25, 2009 at 10:11:38PM +0900, Brian Candler wrote: > Pito Salas wrote: > > David, > > > > Thanks for a sensational answer to my question! Great detail, very > > informative. > > > >> $: << '/path/to/system/lib' > >> require 'sub_alpha' > >> > >> In my opinion, that's much better than trying to do something like: > >> > >> require '/path/to/system/lib/sub_alpha.rb' > > > > I've seen the $: << construct; can you say a little about why it's much > > better than putting the path directly in the require? > > > > My guess is that the path appears only once in the $: line so if it > > changes you don't have to go changing a whole bunch of requires. Yes, or > > is there something else more important? > > If one bit of code does "require 'sub_alpha'", and another bit of code > does "require '/path/to/system/lib/sub_alpha'", then the file will be > loaded twice. Can I suggest that someone doing a require using the entire path is Doing It Wrong™? They should be properly adjusting their load path with -I. Specifying a full path is like saying "Hey Ruby and Rubygems, I either don't understand how load paths work, or I don't trust you to do your job!". > So you may see this pattern: > > require File.dirname(__FILE__)+"/../lib/sub_alpha" > > but I'd strongly recommend against it. > > BTW, $:.unshift is probably better than $: <<, because it will put your > code at the front of the path. Mucking with the load path is bad and is not the responsibility of your script. The include path should be set up by Ruby, Rubygems, or the -I flag to the ruby script IMO. Remember that when you mess with the load path, you are modifying a global variable. -- Aaron Patterson http://tenderlovemaking.com/