Ruby - How to hit LOAD_PATH at development time?

Asked

Viewed 32 times

0

First: I’m a newcomer to Ruby.

I have a Ruby project that obeys the design pattern of the vast majority of projects. Therefore, the main executable is in bin/.

bin/exec
lib/ 
    |
     -- library.rb
     -- library/

The problem is that the main executable depends on the library.Rb to run correctly, and the library.Rb depends on everything else within the library directory.

If I package the project in a Gem and install this Gem, the main executable runs because Rubygems will take care of everything, but this has the disadvantage that the main executable does not see the changes inside the lib directory.

It always leads me to write code like this:

$: << File.expand_path('../lib')

And in library.Rb:

$: << File.expand_path(File.dirname(__FILE__))

This solves the problem, but distributing the software with this code is distributing software with "debug code". And in the worst case this can cause a bug, if some unlucky runs the main executable in a directory where coincidentally there is also a directory called "lib" and by a bigger coincidence there is a file called library.Rb inside the directory.

How to make LOAD_PATH correct at development time without polluting the code?

1 answer

0

I believe your concerns about this code should not exist. Other than what you say, this is the standard procedure for all applications with Ruby executable files: the executable is in bin/ and this adds the ../lib in the load path.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.