49 Days of Ruby: Day 33 - Creating a Ruby Gem
Tags: ruby
Published: 2021-04-29 06:09:09

---
title: 49 Days of Ruby: Day 33 - Creating a Ruby Gem
published: true
description: Join us on a 49 day learning journey through Ruby!
series: 49 Days of Ruby
tags: #ruby
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/9m73qaetuse3nm3y3it8.png
---

**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:

```bash
./my_first_gem
├── my_first_gem.gemspec
└── lib
    └── my_first_gem.rb
```

## 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:

```ruby
class MyFirstGem
  def self.coffee
    puts "Today is an espresso kind of day"
  end
end
```

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](https://guides.rubygems.org/specification-reference/).

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

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

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:

```ruby
s.files = ["lib/my_first_gem.rb"]
```

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:

```bash
$ gem build my_first_gem.gemspec
```

Then, you can install it:

```bash
$ gem install ./my_first_gem-0.0.1.gemspec
```

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](https://guides.rubygems.org/make-your-own-gem/). 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](https://twitter.com/hashtag/49daysofruby).**