Making a Ruby Gem is Easier than You Think

After using countless Ruby Gems, I’ve finally started making my own. The idea is pretty intimidating, but it actually isn’t so different to making your own scripts or Rails apps since Bundler handles almost everything for you.

To set up all the basics for a Ruby Gem (as well as initialize a git repository for it), simply run

$ bundle gem your_gems_name

from your terminal and you’re off to the races!

This will create (among other things) a gem spec file, which is where you list all your dependencies (if any), as well as information (including version) about your gem.

The real meat of your gem (the logic, etc) will go in /lib/ and should all be namespaced for your gem to avoid conflicts with any classes/methods that your users might have written themselves (or even ones you have written yourself for your own apps).

To install your gem to your local machine to test it out and see if it does what you want it to do, simply run

$ rake install

from your terminal while inside your gem’s directory. If/when you are ready to release your (hopefully functioning) gem to RubyGems.org, run

$ rake release

and there it goes! Be sure to appropriately change the version any time you make changes before releasing again, to avoid breaking the gem for people that may have installed previous versions and written all their code based on that.

I decided to start out simply enough, just to get the gist of everything, and made an API wrapper for the SF Parks API (the API itself is at https://data.sfgov.org/api/views/z76i-7s65) because its API is formatted really weird and is deeply nested and hard to traverse. I took it and navigated all the weirdness and made it into a PORO for ease of use in a Rails app. So now, if anybody wants to use the SF Parks API, they can do so quickly and easily.

This got me all fired up about making my own gems, so I’ve already begun work on a rating gem, since I’ve personally noticed a dearth of rating gems out there. There’s ratyrate, but that requires Devise (which I don’t use), so I thought maybe other people might want a rating gem that does not use Devise. I’ve already written my own rating system, so all that remains is to transfer my existing system into the gem structure so others can install it. Hooray!

Author: Steen

Steen is a nerdy biologist who spends a lot of time trying to cultivate Chloroflexi, who also likes to draw comics, play video games, and climb.

One thought on “Making a Ruby Gem is Easier than You Think”

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.