What is a Gem? Part 2
Tags: ruby, programming, gems, beginners
Published: 2019-04-01 23:28:43

---
title: What is a Gem? Part 2
published: true
description: 
tags: ruby, programming, gems, beginners
---

Welcome back to my series on gems in Ruby! [Last time](https://dev.to/clairemuller/what-is-a-gem-part-1-2pf1) I went back to the basics, and learned that a gem is Ruby's version of a package, which is simply a collection of files. Gems are frequently used to add functionality to a Ruby application or the CLI. [RubyGems](https://rubygems.org/) is the package manager for Ruby - there's currently 10,023 gems to choose from!

# Gem Structure

A gem’s structure should look fairly familiar; most come with lib, test/spec, and bin directories, as well as a README, Rakefile, and gemspec. Here’s a tree breakdown of the gem [tux](https://rubygems.org/gems/tux/versions/0.3.0):

![tux gem structure](https://thepracticaldev.s3.amazonaws.com/i/d2gyrrr0m3msbgpzr346.png)

### lib

The lib directory contains all of the code that makes up the program. Standard practice is to create a file with the same name as the gem just inside the lib directory. This file is considered the ‘root’ file, and contains the main module for the program. This is the file that is loaded when you ‘require’ the gem.

Since this root file can easily get crowded, it’s common to create a directory inside lib, also with the same name as the gem. This folder will store all other files related to the module, and will need to be required at the top of the root file.

### test/spec & bin

The test or spec directory is pretty straightforward, it stores all the test files! A marker of a good gem is the inclusion of these files; other developers will trust your code more if it has solid tests. With this we need a Rakefile, which allows us to run ‘rake test’ or simply ‘rake’ to run the test suite. The bin directory contains an executable file that’s loaded into a user’s PATH; the standard is to give this file the same name as the gem.

### Gemspec

![tux gemspec](https://thepracticaldev.s3.amazonaws.com/i/8f36ks6mdtnge2wt9q60.png)

The .gemspec file contains all the information about the gem, including its name, version, files, author(s), and a brief summary. While these fields are required, a gem creator can choose to add additional information, like a contact email, license(s), required ruby version and other metadata.

# Useful Commands

Here are a few commands that I found useful in my learning:

`gem install <gem name>`

Does what it says on the tin, installs the gem! `i` can be used as an alias for `install`.

`gem list`

Shows every gem that is installed locally, with their versions.

`gem update`

Also pretty straightforward! If you run this command without a specific gem name, it will update all your gems to the newest version.

`gem cleanup`

This one's great if you're a little OCD like me. It removes all but the latest version of each gem, unless that version is required elsewhere to meet a dependency. You can also run this command on specific gems.

`gem environment`

This command gives you some useful information, like your RubyGems and ruby versions, as well as where gems are installed on your computer. `env` can be used as an alias for `environment`.

# Conclusion

To summarize what I’ve learned about gems: they’re essentially bundles of code that can improve your projects by adding functionality or cool features. There are a ton of helpful gems out there, and [RubyGems](https://rubygems.org/) is definitely the best resource. Thanks for reading!