Blog

Ari
May 27, 2017
Ruby Tutorial No Comments

Ruby Tutorial: Building Your Own Ruby Gem

For those who have already learn Ruby, or maybe have been a little bit used to programming in Ruby, the so called ‘Ruby gem’ should not be surprising, isn’t it? Well, just to make sure we have a common understanding of Ruby gem is, let’s review it in brief. To make it simple, we would simply define Ruby gem as an ‘add-on’, ‘tools’, that you could add to your own Ruby program to enhance it functionality. Does it sounds too simple? Well, that’s actually one of the purpose of Ruby gem. To make our work become easier and simple. But don’t worry, we will talk about it in details later.

Building your own Ruby gem may seem like a difficult task. Maybe it even feels like a mission impossible for some of us.. But it is actually not so bad. Anyone, as long as we learn and try, could do it. Building a ruby gem is rewarding for you. First reason is because it can enhances your skills in Ruby programming. Not only that, you can also help others by making a useful gem. You will feel how that your contribution is meaningful. You will get happier if you feel your life is meaningful, isn’t it? But actually, what makes building your own gem is so important is to make your coding life with Ruby could be easier. Because if you don’t find an existing gem with the functionality that you needed, you can simply make it by your own.

In this tutorial, we will try and learn on how to make a ruby gem by our own. We will talk about the basic of building a gem in ruby. We will try to give you examples to make sure you’ve understand it clearly. We hope that after reading this ruby tutorial you will have some basic understanding in building ruby gem. So, let’s do it then!

 

# Step 1: Understanding Ruby Gem

Before we proceed on how to create a ruby gem, we believe you should understand what a gem is. Well, we do define it before, but it is not actually define it inside out. Well of course you can skip this part if you think you have understand it though. But for those who wanted to learn it more, we will talk about it in this section. Let’s take a look at our ruby history first.

The concept of having a plugin to our ruby programming actually had existed before we thought about ruby gems. What makes it problematic is, the plugin of the past required the full code hierarchy to be included directly into our own project. When we wanted to update it, we used a manual process too. We need to download and then extract it atop the previous plugin in our project. This increase the possibility of creating a mistake, and reduce the potential for developers to modify plugin code because it was included in the code base. Because of this we need a new kind of ‘plugin’. And this is where ruby gem take its role.

Gem makes our life much easier. We just need to reference the name of the gem, and if you want to, its version if we wanted to use it. We have a Bundler, that acts as a package manager for our application. It determine what the full set of direct dependencies and sub-dependencies that our application needed. The code for the required gems is still downloaded, but it is being separated from our application, so that it don’t need to be maintained in source control. What we do then, is only to manage our gem via a special file named Gemfile.

Basically, a gem would modify the require path of your apps so that whenever it run, the code inside your gem will also play its role. So your work will be done.

 

# Step 2 : Get the basic

#2.1 Creating ruby gem using a gem

There is basically two ways to create your own gem. First, manually creating your gem files, or using a gem called ‘bundler. It isa gem to help you build your own gem. For this tutorial, let’s use it to create our own gem. For this tutorial we will create a gem that will reverse any string you input backward. So, if you type “jack” for example, it will return “kcaj”. Well, it’s not really useful, but let’s stick with it.

So firstly, let’s install bundler gem if you don’t have it installed yet. Go to your ruby terminal, and then type this code below.

gem install bundler

After bundler gem is installed, let’s begin by creating a name of our gem first. Let’s say we want to create a gem named “reverser”. Type this in your terminal.

bundle gem reverser

You will be prompted some questions that you need to ask (it depends on your bundler version). Just answer it as you wish. After that, there will be a new directory of your gem. For example, mine was like this.

Let’s try to understand some of the files bundler created for you. First, open up (your gem name).gemspace file. In my case, it is reverser.gemspace file. This file contain a metadata about your gem. It includes information such as your gem name, author, license and etc. It also provides path information that specifies what files to include when updating the load path, and also when packaging. If you open it, it will look like this.

So, we have some block of codes here. Let’s say that the first one are starting from spec.name until end. While the second group start from spec.files until spec.require_paths. Most of the time you don’t have to edit this group, but if you need additional folders to include then you’ll need to update it.

Next to the second group, there will be another line, which is referencing add_dependency and add_development_dependency. This line will describe what dependencies are needed for your own gem. Add_dependency refers to what is required by your gem at runtime. While Add_development_dependency is a something that is needed during your gem development or testing. You can add any as you wish.

Let’s take another look at lib/reverser.rb and lib/reverser/version.rb. In version.rb, you will see some description of our reverser gem. While in reverser.rb, this will be the place of what your gem would actually do. In other word, your code will be stored in there. Usually, at first it will be defined t a module. But you can change it to another kind of things, such as class. Of course, changing it to a class would also require other files referencing to our gem to be updated too. Our reverser.rb, or whatever your name of your gem file size would depend on what kind of function and task that you want your gem to do. For a smaller gem, like a reversing gem that we will created in this example, typing all of our code in this file is enough. Now, let’s start our coding.

 

#2.1 Build Your Algorithm and Type It Down

For this tutorial, we wanted to make a gem that takes any string we input to the console. So it is just one simple method to do. For another gem you wanted to make, the algorithm might be more complicated, and of course takes more time to build it. For this case of this tutorial, let’s stick with creating a module that will take every string to its reverse version.

# editing in reverser.rb

require ‘reverser/version.rb’

    def self.process(str)

        str = str.reverse

    end

end

 

And there we are. You just finished creating your first gem. Well, at least finished in trying it. If your gem are more complicated, of course your code won’t be this simple. Well, but this is basically all you need to do if you want to build your own gem.

 

#3 Conclusion and Extras

Let’s review what we’ve talked so far. We’ve shown you the basic (and I guarantee you, it is very very basic) step on creating your own ruby gem. First, you need to install a bundler gem (or manually creating your required files). Then you need to create a name for your gem. After that, edit some files that has been created. Create an algorithm and then type it down in your ‘(gemname)’.rb file. And voila, you’ve finished making your own gem.

There are additional steps you can take in this gem development process. For example, you can add a testing method in your gem so that you can check whether your gem would run as you wish. Some of the tool we has are rspec. Or, you can release your gem to git or somewhere else so that others can use it. Hopefully we can cover it in another tutorial. So by now, are you ready to build your own gem?

You can also check other tutorial: Building Stock Market Application 

Comment