An Introduction to Ruby Gems
July 7, 2021 in ruby

While learning Ruby, I came across gems, which are similar to packages in Node. I spent the day exploring how to work with them. This post explains what ruby gems are and various commands you can use the interact with them.

What is a ruby gem?

A gem is a software package containing ruby application or library. Gems give you a structured and reliable way to install, use, package, and distribute ruby code. You can use gems written by other ruby programmers in your own applications and write and publish your own gems that others can use.

What is RubyGems?

RubyGems is a package manager for ruby, providing a standard format for distributing ruby gems. It makes it easy to locate, install, upgrade, and uninstall gems. You can use the gem command that ships with ruby to use RubyGems.

Before RubyGems, if you wanted to install a new library, you had to search the Web, download a package, and attempt to install it—only to find that you had to install its dependencies, too.

Using RubyGems, you can bundle your code into single files called gems. These files conform to a standardized format and typically are stored in repositories on the Internet (but you can also create your own repositories if you want). RubyGems can also install utility programs that you can invoke from the command line.

RubyGems provides gem, a command-line tool for working with Ruby gems. It also provides integration into Ruby so that your programs can access gems as libraries.

Searching for Gems

When you are a new ruby programmer, you won’t be writing your own gems. Instead, you will use the gems developed by others. Here are a few different ways to check what gems are available for you to use.

  1. The search command lets you find gems from the command line.

    ➜  ~ gem search sequel
       
    *** REMOTE GEMS ***
       
    abbish_sequel_plugins (0.0.6)
    active_scaffold-sequel (0.8.0)
    acts_as_api_sequel (0.0.1)
    annotate-sequel (1.1.1)
    aquel-sequel (0.0.1)
    async-sequel (0.1.0)
    bumbleworks-sequel (0.0.4)
    [...]
    

    As you can see, gem returns many entries, all containing the phrase sequel when you only provide the name. You can use regular expressions to be more precise.

    ➜  ~ gem search ^sequel$
       
    *** REMOTE GEMS ***
       
    sequel (5.46.0)
    
  2. Use the search feature on the RubyGems website. For example, this search for the sequel gem.

Installing the Gems

Once you find a gem that suits your needs, the next step is to install it so you can use it in your application. The install command lets you install the gem along with its documentation.

➜  ~ gem install sequel
Fetching sequel-5.46.0.gem
Successfully installed sequel-5.46.0
Parsing documentation for sequel-5.46.0
Installing ri documentation for sequel-5.46.0
Done installing documentation for sequel after 5 seconds
1 gem installed

List Installed Gems

To see a list of all the available gems, use the list command.

➜  ~ gem list

*** LOCAL GEMS ***

abbrev (default: 0.1.0)
actioncable (6.1.4)
actionmailbox (6.1.4)
actionmailer (6.1.4)
actionpack (6.1.4)
actiontext (6.1.4)
[...]

Uninstalling a Gem

If you installed a gem by mistake or no longer need it, you can uninstall it using the uninstall command.

➜  ~ gem uninstall drip
Successfully uninstalled drip-0.1.1

Read the Gem Documentation

To understand how a gem works, you can read the documentation that comes with the gem. To see the documentation for a gem, use the server command, which spins up a server. You can then go to the URL provided to read the documentation of the gem.

RubyGems Server

This was the introduction to gems in ruby. In the next post, we will learn how to create your own gems.