Ruby gems are like little diamonds in the rough of coding — they allow you to add bits of functionality to your projects without having to write the same code or logic over and over again. Do you use rspec tests, bundler and rack when coding with Ruby? These are all gems!
The great thing about Ruby gems is that they’re not just limited to what’s out there. RubyGems.org is an open source site, which means that anyone can push their own gems up for public use. Have a great idea for a gem like, say, to wish everyone a happy birthday? Simply follow the conventions for writing your own gem, push it live and add it to your gemfiles whenever you’d like to implement it!
In this blog post, I’ll walk you through the basic conventions of creating your own gem and adding it to the public domain.
Step 1: Naming & Building Your Gem
The first thing we’ll want to do when creating a gem is to come up with a name and concept, then build out your basic gem. For this example, I’m creating a simple gem to wish someone a happy birthday. RubyGems does not allow duplicates, so make sure that the name you come up with for your gem has not been taken. Once you’ve confirmed that, you’re good to start building your gem!
If you’ve taken a look through RubyGems, it’s clear that we’re definitely not the first to decide to create our own gem. In fact, someone even created a ‘bundler’ gem designed specifically to scaffold our gem for us. Run
gem install bundler to install the bundler gem, then
bundle gem your_gem_name to create a directory for us to work in! Our new happy_bday gem directory looks like this:
Looks great! Time to move onto the next step: our gemspec.
Step 2: Gemspec & Version
If you open up the happybday.gemspec file that bundler has created for us, it will look something like this:
Update the top section — filling in the authors, email, summary and homepage items.
If you navigate to
lib/happy_bday, you’ll see a file called
version.rb. This is automatically generated with bundler and looks like this:
We can leave the version as it is for now. If we decide to expand or add more functionality to our gem later, we’ll update the version here.
Step 3: Coding Your Gem
Now comes the fun part — making our gem work! We’ll be writing all our code in
lib/happy_bday.rb . Here’s what bundler has generated for us, including directions where to input our code:
Cool! Let’s write some basic code that will create a class method to wish the user a happy birthday when called.
Now that we have a basic method for our gem, it’s time to test it out!
Step 4: Testing Your Gem
There are many ways to test out your gem, including using rspec tests or irb. I’ll walk you through how to use irb here, but rspec is another great resource for making sure your code works!
First, we’ll want to build our new gem. Run
gem build your_gem.gemspec (in this case,
gem build happy_bday.gemspec) in your terminal to create your gem. Once you get the success message, we’re ready to install. Let’s run
gem install ./your_gem-version.number.gem to get this started.
Nice! Our gem has been built and installed, so we’re ready to test it’s (super useful) functionality. Drop into irb, and
require 'happy_bday' to make sure our irb session can use our new gem. Here comes the fun part: trying out what we built! Let’s call the happy_bday method and see if it works.
Woohoo! It printed out ‘happy birthday’, which matches the code we wrote. Our gem officially works. Now that it’s up and running, it’s time to push our gem to the world.
Step 5: Pushing Your Gem Live
To push your gem to RubyGems, you’ll need to create an account on the site. Once that has been created, we’ll only need one command to push it live. You’ll be prompted to enter your email and password, then it will be created upon successful login.
Head over to RubyGems.org to see our new gem!
Now, anyone will be able to download an use happy_bday. Pretty cool!
For additional instructions and official documentation, check out the official guide to making your own gem at RubyGems.