How to create a Ruby gem

ruby_gemRuby gems are a way of adding functionality to programs written in Ruby, including the popular web application framework Ruby on Rails. Gems are also a way for people to share their code to the world and contribute to open source.

This is a step-by-step guide to show you how to create and publish your own Ruby gem to Rubygems.org.

Create a git repository

You can use any git service like Github, Gitlab, or Bitbucket for this purpose. In this article we will use Github to share our gem.

gem_github

Ignore the README, LICENSE, and .gitignore as we will generate those later.

Clone your repository

git clone [email protected]:yourname/onepunchman.git

This will clone an (almost) empty repository. To build our gem, we will use Bundler’s gem creator command to get a skeleton framework so that we can start quickly. This gem creator also provides useful defaults which make our life easier when creating Ruby gems.

We start by creating a temporary directory where we will place the generated code of Bundler’s gem builder.

mkdir TEMPORARY
cd TEMPORARY
bundle gem onepunchman

It will ask some questions on what additional files to include:

Do you want to include a code of conduct in gems you generate? Codes of conduct can increase contributions to your project by contributors who prefer collaborative, safe spaces. You can read more about the code of conduct at contributor-covenant.org. Having a code of conduct means agreeing to the responsibility of enforcing it, so be sure that you are prepared to do that. For suggestions about how to enforce codes of conduct, see bit.ly/coc-enforcement. y/(n): y Code of conduct enabled in config Do you want to license your code permissively under the MIT license? This means that any other developer or company will be legally allowed to use your code for free as long as they admit you created it. You can read more about the MIT license at choosealicense.com/licenses/mit. y/(n): y MIT License enabled in config Do you want to generate tests with your gem? Type 'rspec' or 'minitest' to generate those test files now and in the future. rspec/minitest/(none): minitest read more