DEV Community

Claire Muller
Claire Muller

Posted on

What is a Gem? Part 2

Welcome back to my series on gems in Ruby! Last time 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 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:

tux gem structure

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

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 is definitely the best resource. Thanks for reading!

Top comments (0)