DEV Community

aryaziai
aryaziai

Posted on • Updated on

How to Create a Ruby Gem

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"

Step 1

Go to Github.com 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 (Git Tutorial). We now need some files to work with!

Step 2

Create an account on RubyGems.org. Go to edit profile from the top right corner.

Alt Text

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.

Step 3

$ 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

Directory:

Alt Text

Step 4

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."

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

Step 5

Open your lib folder and begin adding functionality to your gem:

Alt Text

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.

Step 6

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

Step 6

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:

RapSongs::Song.play

Takeaways

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 pkg/ 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

Top comments (2)

Collapse
 
tenzinisco profile image
Tenzin dorjee

@aryaziai , where is play method defined?

Collapse
 
bradpotts profile image
Bradley J Potts

You're right this tutorial needs more details too.