How to Create a Ruby Gem
Tags: ruby, gems, creategem
Published: 2019-12-04 21:03:43

Last post I discussed why I found ruby gems interesting, and the steps required in order to install and utilize a gem. This time around I thought it would be interesting to create my own ruby gem. I like to listen to music when I code, so I thought it'd be fun to create a gem that will randomly open a song anytime that I run the gem. I can't think of any creative names at the moment so I'll just call it "rap_songs"


<h1>Step 1</h1>
Go to <a href="https://github.com">Github.com</a> and create a new repository. Clone repo to your computer:
```$ git clone https://github.com/aryaziai/rap_songs_gem```

Afterwards create a new branch, and push updates when it's appropriate <a href="https://rogerdudler.github.io/git-guide/">(Git Tutorial)</a>. We now need some files to work with!

<h1>Step 2</h1>
Create an account on <a href="https://rubygems.org/">RubyGems.org</a>. Go to edit profile from the top right corner.


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

Scroll until you see something similar to the command below: 

```curl -u yourusername......```

Run the command in your console in order to connect your repo to RubyGems.


<h1>Step 3</h1>
```$ bundle gem rap_songs```

This will behave similar to "Rails new" when it comes to generating necessary files. It will also prompt several questions in your console regarding specs and copyright information.

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


<h3>Directory:</h3>

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

<h1>Step 4</h1>
Go to your gemspec file and edit the following lines:

```spec.summary       = %q{TODO: Write a short summary, because RubyGems requires one.}```
 ```spec.description   = %q{TODO: Write a longer description or delete this line.}```
```spec.homepage      = "TODO: Put your gem's website or public repo URL here."```
```spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"```
```spec.metadata["homepage_uri"] = spec.homepage```
```spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."```
```spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."```

<b>Make sure to assign spec.metadata["allowed_push_host"] to https://rubygems.org


<h1>Step 5</h1>
Open your lib folder and begin adding functionality to your gem:

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

I required a few other gems inside my gem: rspec, pry, require_all, and Launchy. Make sure to add these gem dependancies inside your gemspec so when other people install your gem, they won't need to individually require all the gems that you used.


<h1>Step 6</h1>
```rake build
gem push pkg/rap_songs-0.1.0.gem```
(Anytime that you want to release your gem to rubygems, run the same commands again, but be sure to change the gem version number in your gem as well as the gem push command).

Now your gem is live!

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

<h1>Step 6</h1>
Open up a random folder with basic ruby files in it. Add ```gem 'rap_songs'``` inside your Gemfile, and ```require 'rap_songs'``` inside your environment or run file. Now you can use my gem, by running:

<b>RapSongs::Song.play</b>

<h1>Takeaways</h1>
One of the issues I spent a lot of time dealing with was that anytime I pushed a new update to Github & RubyGems, bundle install would keep installing the very first version of my gem. I learned that I needed to add <b>pkg/</b> inside my gitignore so that bundle would ignore previous versions of your gem. 

When it came to triggering my gem, I also learned the syntax by looking at the faker gem's code. I need a module, class, and class method where I'm creating a new instance of the class and running my play method on it. 

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