Adding a directory to $ LOAD_PATH (Ruby)

I saw two commonly used methods for adding a file directory, which is currently running in $ LOAD_PATH (or $ :). I see the benefits of this if you are not working with a gem. Obviously, one of them is more detailed than the other, but is there a reason why you can go with one above the other?

The first, detailed method (there may be an enumeration):

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

and simpler, faster and more dirty:

 $:.unshift File.dirname(__FILE__) 

Is there any reason to go one above the other?

+75
ruby rubygems
May 07 '09 at 9:26 p.m.
source share
6 answers

I would say go with $:.unshift File.dirname(__FILE__) on top of the other, simply because I saw much more use of it in the code than $LOAD_PATH , and it is also shorter!

+45
May 07 '09 at 11:44 p.m.
source share

The Ruby download path is very common as $:, but just because it is short doesn't make it better. If you prefer clarity to skill or if brevity makes you itchy, you don't need to do this just because everyone else is. Say hello ...

 $LOAD_PATH 

... and say goodbye to ...

 # I don't quite understand what this is doing... $: 
+131
Sep 21 '09 at 21:01
source share

I don’t love fast and dirty too much. Anyone familiar with Ruby will reflect on what $:. .

I find this more obvious.

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

Or if I don't care that I have the full path ...

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

UPDATE 2009/09/10

Recently, I have been doing the following:

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

I saw it in a number of different ruby ​​projects while viewing GitHub.

Seems to be an agreement?

+18
Jul 11 '09 at 3:26
source share

If you enter script/console into a Rails project and enter $: you will get an array containing all the directories needed to load Ruby. The conclusion from this little exercise is that $: is an array. Thus, you can perform functions on it, for example, add other directories using the unshift method or the << operator. As you implied in your statement, $: and $LOAD_PATH are the same.

The disadvantage of this is the quick and dirty way, as you mentioned, this: if you already have a directory in your download path, it will be repeated.

Example:

I have a plugin that I created called by todo. My directory is structured like this:

 / --- vendor
   |
   | --- / plugins
         |
         | --- / todo
               |
               | --- / lib
                     |
                     | --- / app
                           |
                           | --- / models
                           | --- / controllers
               |
               | --- / rails
                     |
                     | --- init.rb

In the init.rb file, I entered the following code:

 ## In vendor/plugins/todo/rails/init.rb %w{ models controllers models }.each do |dir| path = File.expand_path(File.join(File.dirname(__FILE__), '../lib', 'app', dir)) $LOAD_PATH << path ActiveSupport::Dependencies.load_paths << path ActiveSupport::Dependencies.load_once_paths.delete(path) end 

Notice how I tell the code block to perform actions within the block in the row models, “controllers” and “models”, where I repeat the “models”. (FYI, %w{ ... } is another way to tell Ruby to contain an array of strings). When I run script/console , I type the following:

 >> puts $: 

And I type this in such a way that reading the contents in a line is easier. The output I get is:

 ...
 ...
 ./Users/Me/mySites/myRailsApp/vendor/plugins/todo/lib/app/models
 ./Users/Me/mySites/myRailsApp/vendor/plugins/todo/lib/app/controllers
 ./Users/Me/mySites/myRailsApp/vendor/plugins/todo/lib/app/models

As you can see, although this is a simple example that I could create using a project that I am currently working on, if you are not careful, a quick and dirty way will lead to repeated paths. A longer path will check for duplicate paths and ensure that they do not occur.

If you are an experienced Rails programmer, you probably have a very good idea of ​​what you are doing, and you probably will not make the mistake of repeating paths. If you are a beginner, I would go with a longer path until you understand what you are doing.

+7
Dec 6 '09 at 19:41
source share

The best I've come across for adding a directory through a relative path when using rspec. I find it quite detailed, but also a pleasant liner.

 $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) 
+3
Nov 05 '15 at 4:09
source share

There is a gem that will allow you to customize your download path with cleaner and cleaner code. Check this out: https://github.com/nayyara-samuel/load-path .

It also has good documentation.

+1
Sep 26 '13 at 1:11
source share



All Articles