Build your own Ruby gem

Ali Erbay
3 min readOct 19, 2020

--

As an avid Ruby on Rails user, setting up and running a Rails API with a test environment pretty much requires utilizing ruby gems all over the place. You can check my article about it here

Did you know that ruby is one of the hardest gemstones?

In this article, we will build a somewhat useful gem from a Codewars kata problem.

Write a function called that takes a string of parentheses and determines if the order of the parentheses is valid. The function should return true if the string is valid, and false if it’s invalid.

Examples;
“()” => true
“)(()))” => false
“(“ => false
“(())((()())())” => true

We are going to modify this problem to include an actual code in them instead of just parentheses. So our gem will notify us if we have mismatching parentheses in our code before we run it.

Creating the Gem scaffold

We are going to use Bundler gem. Bundler provides a consistent environment for Ruby projects by tracking and installing the exact gems and versions that are needed. Be sure that you have it via $ gem install bundler command.

Bundler gives us the ability to create gems and we are going to scaffold our gem firstly.

$ bundle gem CheckParans

You will be prompted with a few yes/no questions, you should select test environment as RSpec because, of course, we are going to test our gem! Then you have a license and code of conduct prompts, you can say no to both because this will be just a test gem.

Auto-generated scaffold files by Bundler

Now we should focus on CheckParans.gemspec file, there is some setup that needs to be done in this file

We must attend to TODO’s and change them accordingly in order to proceed. source_code_uri , homepage_uriand changelog_uri is not needed for now and can be deleted. Let me share with you the latest gemspec;

That should do it! We are ready to write our tests but run $ bundle install on your command line first to get RSpec ready.

Testing with RSpec

Our test file is created when we scaffolded the project, it is located atspec/CheckParans_spec.rb , let’s add some simple tests to that;

If you type $ rspec now it will say that undefined method 'valid_parentheses' as expected, let’s go and define our core functionality to lib/CheckParans.rb

This will check if the parentheses are valid in any given string, return false or true depending on validity. Now if we try to run $ rspec on CLI, all tests should be green!

Build the Gem

Only the easiest part left now, we just need to run the following command;

$ gem build CheckParans.gemspec

Gem file is created and now we can install it like we do with other gem packages

$ gem install CheckParans-0.1.0.gem

Our in house made gem package is ready to use, you can type $ gem list and find installed gem packages in the system. CheckParans is there! (and also I realized that we didn’t obey naming conventions but that’s not the point now)

Use the Gem

Since the gem is installed we can require it any rb file we wish

I decided to check parentheses validity in a particular JavaScript code, our_test_code_2 has an extra invalid parenthese at the end of the code. Our gem provides us functionality to check it, we will receive true and false respectively for our_test_code_1 and our_test_code_2

Next time we will look at how to publish our gem and use it with in command line!

--

--

Ali Erbay

Full Stack Developer BDD/TDD || Ruby on Rails || React || React Native || NodeJS || Express https://github.com/kermit-klein