DEV Community

Cover image for Building and Publishing my first Ruby Gem. (Also a tutorial)
Aweys Ahmed
Aweys Ahmed

Posted on

Building and Publishing my first Ruby Gem. (Also a tutorial)

I have been looking forward to building a Ruby gem for quite some time. I have used them in my projects and wanted to see what is required to build one.

I'm also excited about sharing this process so that you can also build a Ruby Gem. If you choose to do so.

What is a Gem?

A Ruby Gem is a software package that you can download using the command line.

gem install [gem-name]
Enter fullscreen mode Exit fullscreen mode

Ruby Gems extend your code and can reduce the amount of code you will have to write. They can work directly with your source code or help automate some tasks.

Here are examples of Gems that you can install.

  • Rails
  • Bundler
  • Rspec

Getting Started

I decided to build a Ruby gem that contains two Class functions that calculate the future value of an asset and the other function calculates the amount needed to have a certain value in the future.

Here is the link to my gem time_value_money so that you can look at the source code of a completed gem.

The first thing we will need to do is create an account at rubygems.org

After you have done that, you will need to open your terminal and type this command.

bundle gem [gem-name]
Enter fullscreen mode Exit fullscreen mode

The output to your terminal should look like the below image.

Alt Text

The next thing you'll need to do is to push this up to your git account. You'll need your git source code URI to add to your README.md file.

The ReadME.md file will also have a few changes that need to be made.

We'll also need to create a CHANGELOG.md file in the root of your project. This will be used to log any changes to your gem file. The changes can come from either you or others.

Here is the CHANGELOG.md from my gem file to give you an idea of what needs to be included.

Alt Text

After creating the CHANGELOG file, you'll need to open up the gemspec file.

[gem-name].rb
Enter fullscreen mode Exit fullscreen mode

Alt Text

The sections Marked TODO will need to be changed.

You will also want to remove or comment out these lines if you plan on posting it to rubygems.org.

Alt Text

To add functionality to your gem, you'll need to go to this file located at

lib/[your-gem-name]/[your-gem-name].rb
Enter fullscreen mode Exit fullscreen mode

After you have added your code, you'll need to test it to make sure that it is working.

Once you have done that, you'll need to get your gem file ready for deployment.

You'll need to run this command in the terminal.

gem build [gem-name].gemspec
Enter fullscreen mode Exit fullscreen mode

Then you'll need to sign in to rubygems in the command line. This can be done by typing

gem signin

and follow the login instructions.

Then you can finally push your gem to ruby gems by typing

gem push [name-of-gem]-0.1.0.gem
Enter fullscreen mode Exit fullscreen mode

Congratulations, you have published your first Ruby Gem.

Top comments (2)

Collapse
 
ben profile image
Ben Halpern

Congrats to you!

Collapse
 
aweysahmed profile image
Aweys Ahmed

Thank you.