# Creating Your Own Ruby Gem

> In this post we will take our basic code from the previous exercises and package it up as a Ruby gem to install in other projects.

- Canonical: https://dennisokeeffe.com/blog/2022-03-19-creating-your-own-ruby-gem
- Published: 2022-03-19
- Updated: 2022-03-19
- Tags: ruby

This post will demonstrate how to create your own [Ruby Gem](https://rubygems.org/).

It will the first in a three-part series that covers the following:

1. Creating a Ruby gem.
2. Deploying to the Ruby gem repository.
3. Automating deployment with GitHub actions.

It will work off the code written in ["RSpec With GitHub Actions"](https://blog.dennisokeeffe.com/blog/2022-03-18-rspec-with-github-actions).

Source code can be found [here](https://github.com/okeeffed/rspec-github-actions).

## Prerequisites

1. Basic familiarity with [Bundler](https://bundler.io/).
2. Work through ["RSpec With GitHub Actions"](https://blog.dennisokeeffe.com/blog/2022-03-18-rspec-with-github-actions).

## Getting started

We will clone the project `rspec-github-actions` and use Bundler to initialize the project:

```s
$ git clone https://github.com/okeeffed/rspec-github-actions.git
$ cd rspec-github-actions
# Checkout the starting point
$ git checkout 1-rspec-with-github-actions

# Make the related gemspec
$ touch contrived_math.gemspec
```

At this stage, our project is now ready to start working with.

## Updating our Gemspec file

Details about our Gem will be added into the `contrived_math.gemspec` file that was created.

I have filled out my details, and so it looks like so:

```rb
Gem::Specification.new do |s|
  s.name        = 'contrived_math'
  s.version     = '0.0.0'
  s.summary     = 'Hello, World!'
  s.description = 'A simple hello world gem'
  s.authors     = ["Dennis O'Keeffe"]
  s.email       = 'hello@dennisokeeffe.com'
  s.files       = ['lib/contrived_math.rb']
  s.homepage    =
    'https://rubygems.org/gems/contrived_math'
  s.license = 'MIT'
end
```

At this point, we can build the gem locally.

## Building the gem

We can build the gem with a simple `gem build` command.

In the terminal, run the following:

```s
$ gem build contrived_math.gemspec
  Successfully built RubyGem
  Name: contrived_math
  Version: 0.0.0
  File: contrived_math-0.0.0.gem
```

Once built, it will output the file into the current working directory.

We can install that by referencing it directly:

```s
$ gem install ./contrived_math-0.0.0.gem
Successfully installed contrived_math-0.0.0
Parsing documentation for contrived_math-0.0.0
Installing ri documentation for contrived_math-0.0.0
Done installing documentation for contrived_math after 0 seconds
1 gem installed
```

## Testing out our gem

At this point, we can test it our in an interactive environment.

Open up IRB and do just that:

```s
$ irb
irb(main):001:0> require 'contrived_math'
\=> true
irb(main):002:0> ContrivedMath.add(1, 2)
=> 3
irb(main):003:0> ContrivedMath.subtract(1, 2)
=> -1
```

Success!

## Summary

Today's post demonstrated how to create our first Ruby gem.

In the next post, we will demonstrate how to deploy it to the gem package repository.

## Resources and further reading

- [Bundler](https://bundler.io/)
- [Source code](https://github.com/okeeffed/rspec-github-actions)
- ["RSpec With GitHub Actions"](https://blog.dennisokeeffe.com/blog/2022-03-18-rspec-with-github-actions)

_Photo credit: [`joshwithers`](https://unsplash.com/@joshwithers)_
