Adding a directory to $LOAD_PATH (Ruby)

Mark W picture Mark W · May 7, 2009 · Viewed 90.3k times · Source

I have seen two commonly used techniques for adding the directory of the file currently being executed to the $LOAD_PATH (or $:). I see the advantages of doing this in case you're not working with a gem. One seems more verbose than the other, obviously, but is there a reason to go with one over the other?

The first, verbose method (could be overkill):

$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__))) unless $LOAD_PATH.include?(File.expand_path(File.dirname(__FILE__)))

and the more straightforward, quick-and-dirty:

$:.unshift File.dirname(__FILE__)

Any reason to go with one over the other?

Answer

user176788 picture user176788 · Sep 21, 2009

The Ruby load path is very commonly seen written as $: , but just because it is short, does not make it better. If you prefer clarity to cleverness, or if brevity for its own sake makes you itchy, you needn't do it just because everyone else is. Say hello to ...

$LOAD_PATH

... and say goodbye to ...

# I don't quite understand what this is doing...
$: