Shine Bright Like A …Gem?

Julia Zolotarev
5 min readOct 19, 2020

If you’re learning Ruby, you have definitely heard of (and have probably unknowingly used) Ruby gems. If they are as much a mystery to you as they were to me about a week ago (which is to say, the word “gem” only conjures up the image of a glimmering stone and has nothing to do with coding), come along for the ride as we explore them together.

For starters, open your terminal and type:

gem list
How they sparkle!

Can it be?? That is a very long list of gems that (if you’re anything like me) you didn’t even know existed on your computer.

So what is all that?

In short, all a gem is, is a bunch of perfectly packaged Ruby code that gives you access to custom methods somebody else wrote. (The best kind of present!) If you really wanted to, you could access the code inside a gem file using gem unpack [name of the gem], copy all of the code inside, and paste it into your application, and the end result would be the same. Gems just make the process of sharing code that much easier.

These custom methods inside of gems typically have specific functionalities. For example, you might use a gem that has existing methods for authenticating user information inside your application. Rather than having to write a complicated Regular Expression (a powerful tool that would merit its own blog post), you now have access to a method that is already written that will do the work for you.

Want to read this story later? Save it in Journal.

Some of the ones you might have already used are:

  • Prya version of IRB (Interactive Ruby Shell) that allows you to test and debug code. This is a favorite of mine because playing around with your code is very fun, and it makes finding bugs a less painful process.
  • RSpec — a framework for building tests for your code.
  • ActiveRecord —a database mapper; if you’ve worked with Object Relational Mapping, you already know that ActiveRecord is a lifesaver. But did you know it was a gem?

Check out https://rubygems.org/ if you are looking for more gems; there are over 160,000 gems uploaded to RubyGems. It’s all well and good that these gems exist somewhere, but you might be asking yourself how to get any of these gems integrated into your application, and that brings us to RubyGems itself.

RubyGems is a tool that was designed to easily share these libraries (gems) in a standard format, and it was developed in 2004. The interface to use RubyGems is a command line tool, meaning that you interact with it through the command line in your terminal. Slightly confusingly, it is named gem. As of 2007’s release of Ruby 1.9 it is included when you download Ruby. To use it to install gems, all you have to do is type gem install [the name of the gem you want to install]. RubyGems will then go ahead and install that gem for you from its library.

But imagine a scenario where you are pair-programming an application that uses 14 different gems (you are a programmer after all, and you like working smarter, not harder!) You’ve already downloaded the gems, and are using them in your application. But when your partner accesses the application to work on it, they won’t know which gems you’re using, and might not have them installed. The application won’t function properly without those gems. What’s a pair to do? Here is where bundler comes in.

Setting up my coding environment in advance of my first week at Flatiron School, I had to run the following code in my terminal:

gem install bundler

Here we’re calling on RubyGems command line tool “gem”, and telling it to install a gem called bundler.

Bear with me, because it’s going to get a bit meta. Bundler is a gem… that installs other gems… which sometimes have dependencies on other gems.

What bundler does is it goes to your application’s Gemfile file (every application using gems has one in the root directory), and installs the listed gems from the specified source at the top all in one fell swoop. Most commonly, it will be https://rubyrems.org but it could also be other sources like GitHub. So now anybody opening your application just needs to run the command bundle install (assuming they have the bundler gem installed) and every single gem in the Gemfile will be installed or updated to the correct version. Pretty amazing.

gemfile example

It is worth noting that you do need to require the gems in the files where you are using them as illustrated below in order to actually utilize the functionality of said gems.

gemfile requirements

If you’re wondering how you might know what beautiful, code-enhancing methods are wrapped into the gift that is the gem you’re using, you will want to check the documentation on the gem itself. For example, if you’re trying to figure out exactly what Pry can do, you might do a Google search for “pry gem documentation,” which will lead you to this page that tells you what is available to you while using Pry. I was surprised to find that Pry is not only useful for pausing your code to peek in and see what’s going on, as I originally thought.

Pry is also customizable so you can create the debugging environment that will be most helpful to you. Entering into a pry session, you have the option to jump around different pieces of code, and also just use it as a basic IRB session.

Eventually, there might come a time when you might want to write your own Ruby gems. If you’re finding that you’re using the same piece of code over and over again in different applications, you can use the instructions here to build a custom gem: https://guides.rubygems.org/make-your-own-gem/. But that is a topic for another blog post :)

More from Journal

There are many Black creators doing incredible work in Tech. This collection of resources shines a light on some of us:

--

--

Julia Zolotarev

Software Engineer; hospitality enthusiast; lover of ice cream.