DEV Community

Cover image for 49 Days of Ruby: Day 33 - Creating a Ruby Gem
Ben Greenberg
Ben Greenberg

Posted on

49 Days of Ruby: Day 33 - Creating a Ruby Gem

Welcome to day 33 of the 49 Days of Ruby! 🎉

Yesterday, we discussed what Rubygems is as a service. Today we will break down how to create your own gem.

As a recap, a gem is a packaged piece of code that can be downloaded and installed into your code to extend its functionality. Gems exist to help with a wide array of things, like authentication, styling, dashboards, etc.

How do you create your own gem?

File Structure

In your terminal, you can create a new directory called my_first_gem.

Within that directory, create a file called my_first_gem.gemspec

Then, add another directory called lib. Inside, the lib directory add a file called my_first_gem.rb.

After you have finished your file structure should look like this:

./my_first_gem
├── my_first_gem.gemspec
└── lib
    └── my_first_gem.rb
Enter fullscreen mode Exit fullscreen mode

Add Some Functionality

The functionality for your gem will be inside the my_first_gem.rb file. Let's have it tell us about coffee today:

class MyFirstGem
  def self.coffee
    puts "Today is an espresso kind of day"
  end
end
Enter fullscreen mode Exit fullscreen mode

Now your gem will output "Today is an espresso kind of day". It's not the most functional of applications, but it is an application.

Define the Specifications

In the top-level folder, we added a file that had a .gemspec extension. This is the gem specification file for our gem.

There are specific fields required and a lot of optional parameters. You can see the details here.

At the most basic, you'll need the following:

Gem::Specification.new do |s|
  s.name        = "my_first_gem"
  s.version     = "0.0.1"
  s.summary     = "Prints out espresso whenever you want"
  s.description = "This gem outputs a string about coffee"
  s.authors     = ["Your Name"]
  s.email       = "Your email"
  s.files       = ["lib/my_first_gem.rb"]
  s.homepage    = "your gem's homepage link"
  s.license       = "MIT"
end
Enter fullscreen mode Exit fullscreen mode

In the above, we specify the name of our gem, a version number, a summary and description, and who we are.

We also, tell the gem where to find the code:

s.files = ["lib/my_first_gem.rb"]
Enter fullscreen mode Exit fullscreen mode

This is very important because, without it, the gem would not know what to do!

Running Your Gem

To run your code as a gem, you need to build it:

$ gem build my_first_gem.gemspec
Enter fullscreen mode Exit fullscreen mode

Then, you can install it:

$ gem install ./my_first_gem-0.0.1.gemspec
Enter fullscreen mode Exit fullscreen mode

Now, it's ready to use locally!

If you want to get even more details, I highly recommend following this guide on the Rubygems site. You will see the same steps, with more explanation, and great resources to continue the discussion.

Come back tomorrow for the next installment of 49 Days of Ruby! You can join the conversation on Twitter with the hashtag #49daysofruby.

Top comments (0)