Question

I am starting a new project which will have the need for a CLI.

I have created a new RubyGem project using

bundle gem roar

So I have structure in place.

To have the CLI I have created a 'bin' folder and added a 'roar' file with the following contents.

require 'roar'

test = Test::Talk.new
test.say_hello

I now want to run the 'roar' bin file that I have created, but if I run the following

ruby bin/roar

I get an error stating that ruby cannot find the gem 'roar'. I am assuming that this is because I am not in the 'environment' so it does not know how to find my library. So how can I run this so that I can execute the scripts I am writing?

I have a feeling that I can create a helper script for use with IRB, but I cannot work out how to do it. Any suggestions are gratefully received.

(I am not attempting to circumvent the need for tests, I am trying to get my head around the CLI and I do not want to have to go through the install gem and execute cycle).

Thanks very much,

Russell

UPDATE:

As requested, the layout of my project looks like:

├── Gemfile
├── Gemfile.lock
├── LICENSE.txt
├── README.md
├── Rakefile
├── bin
│   └── roar
├── lib
│   ├── roar
│   │   └── version.rb
│   └── roar.rb
├── roar.gemspec
├── spec
│   ├── roar_spec.rb
│   └── spec_helper.rb
└── tmp
Was it helpful?

Solution

Normally when you run an executable from a gem Rubygems will set up the LOAD_PATH for you so that it contains your gems’ lib directory. Obviously when you run the script in development Rubygems doesn’t get the chance to set things up for you so you will need to do it yourself.

One way to do this is in the script file itself. For example the Haml and Redcarpet gems do this. Somewhere near the top of your executable you would have something like this:

$LOAD_PATH.unshift File.dirname(__FILE__) + '/../lib'

(assuming a fairly typical directory layout).

Another way, if you want to avoid manipulating the load path in code, would be to add the lib dir in the command line. The -I option to the ruby command lets you do this, so you could run:

$ ruby -Ilib/ bin/roar

from the top level of your gem.

Alternatively you could use the RUBYLIB environment variable:

$ RUBYLIB=lib ruby bin/roar

or even (if bin/roar has a shebang line and is executable):

$ RUBYLIB=lib ./bin/roar

You could even export RUBYLIB so you didn’t need to specify it each time, but you would need to be careful about leaving it set if you did that.

OTHER TIPS

Can you share your dir tree to clarify some things? I'm curious what your lib looks like.

But if you're trying to use your own gem, the simplest answer is that you need to gem install within the directory that you're making it in. It was probably breaking because you couldn't require your own gem that hadn't been installed. Try running gem install roar in your command line in the top level of roar/. Once you've run gem install roar simply call roar. The annoying part is that if it breaks somewhere else you will have to reinstall the gem every time you change something to test it.

Here's the github page for a simple gem I just made that might help you see the correct layout in action, https://github.com/TSiege/nytimes-top-story-gem. It's readme might help clarify how to use a gem you make yourself.

If you are currently developing a gem I would suggest utilizing bundler's :path option in the Gemfile and point to your unpacked library. This will allow you to work on the Gem while running your scripts.

I would suggest pointing the path directly to your local unpacked rubygem library. Then running bundle install to ensure it's pointing to the correct path. Also, you will need to reload any running processes to grab changes from the unpacked gem library.

recommended via bundler.io:

gem 'roar', :path => './vendor/extracted_library'

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top