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, come along for the ride as we explore them together.

What Are Gems in Ruby on Rails?

A gem is a bunch of perfectly packaged Ruby code that gives you access to custom methods somebody else wrote. Gems just make the process of sharing code much easier.The 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, you now have access to an already-written method that will do the work for you.

For starters, open your terminal and type:

gem list

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. 

what-are-gems-ruby-on-rails

So what is all that?

More From Julia ZolotarevCreate React App and TypeScript — A Quick How-To

 

What Are Gems in Ruby on Rails?

In short, a gem is a bunch of perfectly packaged Ruby code that gives you access to custom methods somebody else wrote. It’s 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, paste it into your application and the end result would be the same. Gems just make the process of sharing code that much easier.

The 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, you now have access to an already-written method that will do the work for you.

Some gems you might have already used are:

  • Pry a 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 fun and Pry 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’s a gem?

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 own application. That brings us to RubyGems itself.

What Are Ruby Gems?

More Software Engineering Tutorials From Our Experts5 Ways to Check If an Object Is Empty in JavaScript

 

What Is RubyGems?

RubyGems is a tool that was designed to easily share these libraries (gems) in a standard format; 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. Somewhat confusingly, it is named gem. As of 2007’s release of Ruby 1.9 it is included when you download Ruby. To use RubyGems 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.

Bundler 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 in one fell swoop. Most commonly, it will be https://rubyrems.org but it could also be other sources like GitHub. 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. 

what-are-gems-ruby-on-rails
Image: Author

It’s worth noting that you do need to require the gems in the files where you’re using them as illustrated below in order to actually use the full functionality of said gems.

what-are-gems-ruby-on-rails
Image: Author

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’ll 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.

Finally, there might come a time when you 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 these instructions to build a custom gem.

Expert Contributors

Built In’s expert contributor network publishes thoughtful, solutions-oriented stories written by innovative tech professionals. It is the tech industry’s definitive destination for sharing compelling, first-person accounts of problem-solving on the road to innovation.

Learn More

Great Companies Need Great People. That's Where We Come In.

Recruit With Us