Adding a directory to $LOAD_PATH (Ruby)

Viewed 95520

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?

8 Answers

I would say go with $:.unshift File.dirname(__FILE__) over the other one, simply because I've seen much more usage of it in code than the $LOAD_PATH one, and it's shorter too!

I'm not too fond on the 'quick-and-dirty' way. Anyone new to Ruby will be pondering what $:. is.

I find this more obvious.

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

Or if I care about having the full path...

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

UPDATE 2009/09/10

As of late I've been doing the following:

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

I've seen it in a whole bunch of different ruby projects while browsing GitHub.

Seems to be the convention?

My 2¢: I like $LOAD_PATH rather than $:. I'm getting old... I've studied 92,000 languages. I find it hard to keep track of all the customs and idioms.

I've come to abhor namespace pollution.

Last, when I deal with paths, I always delete and then either append or prepend -- depending upon how I want the search to proceed. Thus, I do:

1.times do
  models_dir = "#{File.expand_path(File.dirname(__FILE__))}/models"
  $LOAD_PATH.delete(models_dir)
  $LOAD_PATH.unshift(models_dir)
end

I know it's been a long time since this question was first asked, but I have an additional answer that I want to share.

I have several Ruby applications that were developed by another programmer over several years, and they re-use the same classes in the different applications although they might access the same database. Since this violates the DRY rule, I decided to create a class library to be shared by all of the Ruby applications. I could have put it in the main Ruby library, but that would hide custom code in the common codebase which I didn't want to do.

I had a problem where I had a name conflict between an already defined name "profile.rb", and a class I was using. This conflict wasn't a problem until I tried to create the common code library. Normally, Ruby searches application locations first, then goes to the $LOAD_PATH locations.

The application_controller.rb could not find the class I created, and threw an error on the original definition because it is not a class. Since I removed the class definition from the app/models section of the application, Ruby could not find it there and went looking for it in the Ruby paths.

So, I modified the $LOAD_PATH variable to include a path to the library directory I was using. This can be done in the environment.rb file at initialization time.

Even with the new directory added to the search path, Ruby was throwing an error because it was preferentially taking the system-defined file first. The search path in the $LOAD_PATH variable preferentially searches the Ruby paths first.

So, I needed to change the search order so that Ruby found the class in my common library before it searched the built-in libraries.

This code did it in the environment.rb file:

Rails::Initializer.run do |config|

* * * * *

path = []
path.concat($LOAD_PATH)
$LOAD_PATH.clear
$LOAD_PATH << 'C:\web\common\lib'
$LOAD_PATH << 'C:\web\common'
$LOAD_PATH.concat(path)

* * * * *

end

I don't think you can use any of the advanced coding constructs given before at this level, but it works just fine if you want to setup something at initialization time in your app. You must maintain the original order of the original $LOAD_PATH variable when it is added back to the new variable otherwise some of the main Ruby classes get lost.

In the application_controller.rb file, I simply use a

require 'profile'
require 'etc' #etc

and this loads the custom library files for the entire application, i.e., I don't have to use require commands in every controller.

For me, this was the solution I was looking for, and I thought I would add it to this answer to pass the information along.

Related