Basics

Use of common RubyGems commands.

The gem command allows you to interact with RubyGems. Ruby 1.9 and newer ships with RubyGems built-in.

The search command lets you find remote gems by name. You can use regular expression characters in your query:

If you see a gem you want more information on you can add the details option(-d). You'll want to do this with a small number of gems, though, as listing gems with details requires downloading more files:

You can also search for gems on rubygems.orgarrow-up-right such as this search for rakearrow-up-right.

gem install

You can use i command instead of install. e.g. gem i GEMNAME

The install command downloads and installs the gem and any necessary dependencies then builds documentation for the installed gems.

Here the drip command depends upon the rbtree gem which has an extension. Ruby installs the dependency rbtree and builds its extension, installs the drip gem, then builds documentation for the installed gems.

You can disable documentation generation using the --no-doc argument when installing gems.

Default options:

gem uninstall

The uninstall command removes the gems you have installed:

If you uninstall a dependency of a gem RubyGems will ask you for confirmation:

gem list

The list command shows your locally installed gems:

Ruby ships with some gems by default, bigdecimal, io-console, json, minitest, psych, rake, rdoc, test-unit for ruby 2.0.0.

gem which <FILE>

Find the location of a library file you can require.

Differences from which and rbenv which:

gem info <GEMNAME>

Prints information about the gem such as name, description, website, license and installed paths

gem environment

Display information about the RubyGems environment.

gem environment gemdir

Display the path where gems are installed.

gem environment gempath

Display path used to search for gems.

gem environment remotesources

display the remote gem servers.

gem environment -h

gem -h

gem help <COMMAND>

You can also use gem <COMMAND> -h

Display help information for a command. e.g.

require gem in code

RubyGems modifies your Ruby load path, which controls how your Ruby code is found by the require statement. When you require a gem, really you're just placing that gem's lib directory onto your $LOAD_PATH.

Let's try this out in irb and get some help from the pretty_print library included with Ruby:

Tip: Passing -r to irb will automatically require a library when irb is loaded.

By default you have just a few system directories on the load path and the Ruby standard libraries. To add the awesome_print directories to the load path, you can require one of its files:

Note: For Ruby 1.8 you must require 'rubygems' before requiring any gems.

Once you've required ap, RubyGems automatically places its lib directory on the $LOAD_PATH.

That's basically it for what's in a gem. Drop Ruby code into lib, name a Ruby file the same as your gem and it's loadable by RubyGems. (for the gem "freewill" the file should be freewill.rb, see also name your gemarrow-up-right)

The lib directory itself normally contains only one .rb file and a directory with the same name as the gem which contains the rest of the files.

For example:

ri: viewing Documentation

You can view the documentation for your installed gems with ri:

fetch and unpack gems

If you wish to audit a gem's contents without installing it you can use the fetch command to download the .gem file then extract its contents with the unpack command.

You can also unpack a gem you have installed, modify a few files, then use the modified gem in place of the installed one:

  • The -I argument adds your unpacked rake to the ruby $LOAD_PATH which prevents RubyGems from loading the gem version (or the default version).

  • The -S argument finds rake in the shell's $PATH so you don't have to type out the full path.

Structure of a gem

Each gem has a name, version, and platform.

For example, the rakearrow-up-right gem has a 13.0.6 version (Released on July 09, 2021). rake's platform is ruby, which means it works on any platform Ruby runs on.

Platforms are based on the CPU architecture, operating system type and sometimes the operating system version. Examples include "x86-mingw32" or "java". The platform indicates the gem only works with a ruby built for the same platform.

RubyGems will automatically download the correct version for your platform. See gem help platform for full details.

Inside gems are the following components:

  • Code (including tests and supporting utilities)

  • Documentation

  • gemspec

Each gem follows the same standard structure of code organization:

Here, you can see the major components of a gem:

  • The lib directory contains the code for the gem

  • The test or spec directory contains tests, depending on which test framework the developer uses

  • A gem usually has a Rakefile, which the rakearrow-up-right program uses to automate tests, generate code, and perform other tasks.

  • This gem also includes an executable file in the bin directory, which will be loaded into the user's PATH when the gem is installed.

  • Documentation is usually included in the README and inline with the code.

  • The final piece is the gemspec, which contains information about the gem.

    • The gem's files, test information, platform, version number and more are all laid out here along with the author's email and name.

The gemspec

The gemspec specifies the information about a gem such as its name, version, description, authors and homepage.

Here's an example of a gemspec file.

Last updated