Building and Publishing my first Ruby Gem. (Also a tutorial)
Tags: tutorial, ruby
Published: 2020-01-27 13:57:45

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]
```

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. 

<ul>
<li>Rails</li>
<li>Bundler</li>
<li>Rspec</li>
</ul>

## 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](https://rubygems.org/gems/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 <a href="https://www.rubygems.org">rubygems.org</a>


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

```
bundle gem [gem-name]
```

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

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/5zk3wn86vim2vghbk760.png)

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](https://thepracticaldev.s3.amazonaws.com/i/n5sf0m7lx55rrn7xyelc.png)

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

```
[gem-name].rb
```

![Alt Text](https://thepracticaldev.s3.amazonaws.com/i/f7x5csjdkp02ronmds22.png)

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](https://thepracticaldev.s3.amazonaws.com/i/joqxw5mjt634ufa29qxb.png)


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

```
lib/[your-gem-name]/[your-gem-name].rb
```

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
```
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
```

Congratulations, you have published your first Ruby Gem.