Gem development — troubles and solutions

I would like to take a look at some of problems that I got stuck on during development of Active Explorer gem. I described the gem creation in the last blog post and this blogpost will be more about traps that you might encounter and tips that should help you.



Local Gem Deployment

I needed to test my gem in other project to confirm that it works. To do so you just add the gem to your Gemfile with `path` attribute that point to gem’s local location.

 # Gemfile - BAD TRY :( gem 'active_explorer', path: '/Users/maxx/Development/Playground/Ruby/active_explorer'

This didn’t work and I was receiving following message:

 Could not find gem 'active_explorer' in source at `/Users/maxx/Development/Playground/Ruby/active_explorer`. (Bundler::GemNotFound) Source does not contain any versions of 'active_explorer'

The sentence "Source does not contain any versions of 'active_explorer'" suggests that there is no version of the gem, no gem at all. However, what it means is that you didn't specify what version you want. Pretty confusing.

The solution was really simple but it took me some time to figure this out. Just add version:

 # Gemfile - CORRECT ONE :) gem 'active_explorer', '0.0.9', path: '/Users/maxx/Development/Playground/Ruby/active_explorer'


I discovered a great gem from Thoughtbout that is called Appraisal.

It helps you to test against different versions of other gems. And that is something that you would likely use when developing your own gem. In my case I needed to test against several Active Record versions (4.x, 5.x).

You just create `Appraisals` file and fill it with gems that should be used in different runs. My `Appraisals` file looks like this:

 # Appraisals appraise "activerecord-4-0" do gem "activerecord", "~> 4.0.13" end
 appraise "activerecord-4-1" do gem "activerecord", "~> 4.1.15" end
 appraise "activerecord-4-2" do gem "activerecord", "~> 4.2.6" end
 appraise "activerecord-5-0" do gem "activerecord", "~> 5.0.0.rc1" end

By testing with appraisel I discovered that my gem only works for Active Record 4.2 (version I used in development) and higher.

How to run the Appraisal?

First you configure your `Appraisals` file and then run:

 $ appraisal install $ appraisal rspec

The Appraisal generates several Gemfiles and runs `rspec` against each of them. Very neat!

To test only specific version add its name:

 $ appraisal activerecord-4-0 rspec

RSpec Notification Center

The gem rspec-nc will connect rspec with your Mac’s Notificaton Center and notifies you about (un)successful run. Quite useful gem especially when combined with Appraisal.

Generating project skeleton

When it comes to starting you project Bundler provides excatly what you need to start.


 $ bundle gem my_gem

You get the whole structure with folder and files generated. Including gemspec file and git repository and.

Caveat — gemspec and local git

After you generate the skeleton, take a look at `gemspec` file. Notice `spec.files` line:

 # my_gem.gemspec spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }

The `spec.files` expression tell gem builder which files to pack together. In this case the list is dynamically generated based on files in your Git.

It works flawlessly with one small exception — new files that haven’t been added to git yet (they are untracked).

When you forget about this fact and you build your gem and test it locally in other project you will have a problem. The gem will not contain all files you would think it should.

That’s all. I hope it helps you to overcome problems with gem development a little bit quicker ;)

Marek Uličný, June 24, 2016