This blog post was inspired by @garybernhardt’s tweet

Coming in a few minutes: my first actual Ruby gem. If I can figure out how to publish it.

In the distant past, I would have suggested to Gary that he use Jeweler when starting a new Ruby Gem. Then one day I was deep in a conversation with Durran Jordan (@modetojoy) about some minutae of MongoDB and Mongoid when the topic of starting a new gem came up. Durran’s reaction was stated with his usual aplomb:

Jeweller? Hoe? F*#!k that noise. Write your own gemspec and be done.

When Durran says something, it is worth listening to. I went back and ripped out Jeweler. The world did not end. I published my gem. I even found I had a bunch of unnecessary baggage. Getting rid of Jeweler actually improved my understanding of the gem publishing process.

How to publish your gem

Publishing a gem is basically a three step process:

Gem layout and Gemspec generation

First push

Subsequent pushes

Well, except for the zeroth step: deciding on a gem name. It is well worth spending a little time googling and taking a look at existing RubyGems.org before choosing your name.

Gem layout and Gemspec generation

The hard way to do this is to find another gem and copy its gemspec and layout.

Or we can cheat.

Bundler will generate a gemspec and a layout for you . And since we are cheating already, lets use use rvm as well, shall we? . You can follow along with this not-yet-released gem handlebars_assets . Here is what we are going to do:

Create a gemset for your_gem

Install bundler

Create the gem layout

Create the .rvmrc

Remove Gemfile.lock from .gitignore

from Make the initial commit

Edit the gemspec as needed

Install all your dependencies

Commit your changes

Easy!

% rvm gemset create your_gem % rvm gemset use your_gem % gem install bundler % bundle gem your_gem % cd your_gem % cat > .rvmrc rvm use default@your_gem

Edit .gitignore to remove Gemfile.lock from the list of ignores.

% git add . % git commit -m 'Initial commit'

We now need to edit our generated gemspec. There are three basic parts to the gemspec:

Metadata

Manifest

Dependencies

Our generated gemspec has placeholder metadata, a good starting manifest, and no dependencies.

1 2 $: .push File .expand_path( " ../lib " , __FILE__ ) 3 " your_gem/version " 4 5 Gem :: Specification .new do |s| 6 7 " your_gem " 8 YourGem :: VERSION 9 " Your Name " ] 10 " your_name@example.com " ] 11 " " 12 %q{ TODO: Write a gem summary } 13 %q{ TODO: Write a gem description } 14 15 " your_gem " 16 17 18 ` git ls-files ` .split( "

" ) 19 ` git ls-files -- {test,spec,features}/* ` .split( "

" ) 20 ` git ls-files -- bin/* ` .split( "

" ).map{ |f| File .basename(f) } 21 " lib " ] 22 23 24 25 26 27 end

Edit the metadata to include your name, email, and what the gem is about. Unless you are doing something off the reservation, there should be no need to mess with the manifest.

The gemspec allows you to specify both runtime and development dependencies. Runtime dependencies are what the users of your gem will need installed with the gem. Development dependencies are used by developers working on your gem, for example gems used for testing only such as RSpec and Cucumber. Add the dependencies you know. You can change the dependency list later easily. Important! Do not edit the Gemfile , bundler knows how to read the gemspec.

% bundle % git commit -a

Now comes the hard part, actually writing your gem.

First push

After you have written you gem’s initial code, you will want to push it out for people to use. There are two things to know about this process:

You need a RubyGems.org account

It is ridiculously easy

Once you have your RubyGems account, lets build and push the gem using the gem command. When you push, you will be asked to log into RubyGems one time. After that, your credentials will be saved for future use.

% gem build your_gem.gemspec % gem push your_gem-0.0.1.gem Enter your RubyGems.org credentials. Don't have an account yet? Create one at http://rubygems.org/sign_up Email: your_name@example.com Password: Pushing gem to https://rubygems.org... Signed in. Pushing gem to https://rubygems.org... Successfully registered gem: your_gem (0.0.1)

Congratulations on publishing your gem!

Subsequent pushes

On subsequent pushes you will need to manage your version number. There are also two things to know about this process:

The version number is in lib/your_gem/version.rb

It is ridiculously easy

Update (but do not commit) your version number in lib/your_gem/version.rb . If you have any other outstanding changes, commit those first — we want our version number commit to be atomic and we are going to tag it. Now do this:

% bundle % git commit -am 'v0.0.2' % git tag 'v0.0.2' % gem build your_gem.gemspec % gem push your_gem-0.0.2.gem

Running bundle before the commit updates the version numbers in the Gemfile.lock file. Your git history will look nice and clean with your spiffy new atomic version commits!

Durran was right: just write the gemspec and be done.