The Basics of a Ruby Gem

Tiffany Carter
5 min readFeb 25, 2019

--

I’ve been using ruby gems for a few weeks now, and it’s been great, but I wasn’t really sure what was happening under the hood when I added one to my application. So this blog post is to help me understand how gems work and hopefully help you too.

So what is a Ruby Gem?

A ruby gem is packaged up ruby code that you can add to your application. This can save you some time by using code that others have written to solve similar problems. For example, if you wanted to change the color of some text, you could install and use the colorize gem. You get all of its functionality without ever having to write that code yourself.

Sinatra is a gem, Rack is a gem, and I was surprised to learn that even Rails is a gem. All public gems are published through RubyGems.org. This is where people can contribute and share them with the community.

Let’s start by installing a gem

Pick a gem and let’s install it. I chose to add the colorize gem to my application, which changes the color of a string we pass in. You can run gem install gem_name to download the gem to your computer and make it available to your applications. Wondering where that gem lives after its installed? You can run gem which gem_name and it will tell you exactly where it is.

gem install colorize

In order to use the gem in our code you will need to require the gem at the top of your file by putting require ‘gem_name’. We do this so that we have access to the gem and now we won’t have to write our own code to colorize text.

require ‘colorize’

Now that you have the gem installed and required, you can use it. Typically, if you go to the gem’s GitHub repo, you’ll find important information about its features and how to use them.

https://github.com/fazibear/colorize

Let’s use it!

So we read the readme and now we want to add some color to our text. I’ve added three different colors in my code, by calling .colorze(:color) on a string.

Running the code

Great! Doesn’t that look nice? So we installed our gem and it seems to be working, but what if we have a bigger application that requires many gems? Calling gem install and requiring each gem seems like a lot of work. Surely there is an easier way.

Bundler

Bundler makes our lives much easier. What is bundler? Well it’s a gem of course! It tracks each gem and installs the exact gem and version that you need to use in your application. In order to use bundler, you’ll need a Gemfile in the directory of your project and inside you can list as many gems as you’d like. To do that, just open the Gemfile and add gem ‘gem_name’.

Once you’ve added it to your Gemfile, be sure to run bundle install in your terminal. This will install all the gems listed in your Gemfile, along with their dependencies. That certainly makes things much easier. What’s great is that Ruby 2.6 and later includes bundler automatically, so you won’t need to install it yourself.

It seems we still need to add require ‘gem_file’ at the top of our files, and that also seems like a lot of work if we have many of them. Turns out, bundler thought of this as well. You can add require ‘bundler’ to the top of your .rb file and write Bunlder.require(:default). What this is doing under the hood is calling require ‘gem_name’ for each gem that you have in your gemfile.

Bundler.require(:default)

Once you have run bundle install, you may notice that a lot of gems you didn’t add are being being installed. Why is that? Many gems require other gems to be installed along with them. For example, rails will require many other gems and those gems may require others gem and so on. Bundler will make sure all the installed gem versions are compatible with one another.

After finding the correct versions of each gem, it records those versions into another file called Gemfile.lock. It makes it possible to install the exact same version on to every machine that will need to run this application.

A Gemfile.lock file

You can see in my Gemfile.lock that lolize requires the paint gem and the versions that it can use. So bundler will grab that and the correct version for us. Isn’t that helpful? Thanks bundler!

So that’s it! I hope this has helped you understand the basic of a gem and bundler. There are many gems out there that you can use and you can search for them here. https://rubygems.org/.

--

--