0

I am making a ruby gem with an executable and a class library. The executable has require "fpa/chklinks" which is the class library in lib/fpa directory.

I get the following error when I run my executable named chklinks.rb:

$ bin/chklinks.rb -h /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require': cannot load such file -- fpa/chklinks (LoadError) from /usr/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require' from bin/chklinks.rb:7:in `<main>' 

How can I make it find my library during development? I have tried require_relative "../lib/fpa/chklinks"

2 Answers 2

1

I fixed the problem by adding the following to my executable above my require statements:

$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)

or

As per David Grayson's post $LOAD_PATH << 'lib'

While this did the trick. Is there a better place for it, as I assume I will have to comment out this line before production?

Thanks

Sign up to request clarification or add additional context in comments.

1 Comment

I don't think you should add any code like that to your executable because it would need to be removed before publishing them gem. You should be able to use the -Ilib option when running your executable in development mode, assuming you are running it from the top level of your project and that the lib directory there is the top level for the library files provided by your gem.
0

Your gem should have a lib directory. During development, you will need to get that lib directory added to Ruby's load path one way or another.

You can add it in Ruby code. I wouldn't recommend doing that in your situation, but it is helpful to put something like this in your test helper or spec helper files to help your tests find the library:

$LOAD_PATH << 'lib' 

You can add it with a Ruby command-line option or environment variable:

RUBYOPT="-Ilib" ruby bin/chklinks.rb ruby -Ilib bin/checklinks.rb 

I'm assuming the commands above would be run from the root directory of your gem, so the path you need to add is simply lib. If your setup is different, you would change lib in the commands above to be some other path.

4 Comments

Thanks for the quick response. I put $LOAD_PATH << 'lib' into my spec_helper.rb file. I am still getting the same error. I must be missing something. When I run bundle I get Using fpa-chklinks 0.1.0 from source at . . Does this mean anything helpful?
Also, the two Ilib commands didn't work either. I tried ruby -Ilib lib/fpa/chklinks.rb as well, assuming that the path should have been to my library file.
It's very likely you are doing something wrong, so you should give as many details as possible, including shell sessions that show where all your files are and what results you get when running various commands. I think you should focus on making the ruby -Ilib bin/checklinks.rb command work, and that really is the command I think you should use. The point is that you want to run bin/checklinks.rb, but when you run it, you want your lib directory to be on the include path. The command you posted in your last comment does not look good.
GOT IT!!! My fault... I woke up this morning with a fresh head and tried ruby -Ilib bin/chklinks.rb which worked. I was using checklinks.rb which was not the name of the file. After seeing it work I get the command now. Quite simple. Thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.