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.
gem search
gem searchThe 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.org such as this search for rake.
gem install
gem installYou can use
icommand instead ofinstall. 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
gem uninstallThe uninstall command removes the gems you have installed:
If you uninstall a dependency of a gem RubyGems will ask you for confirmation:
gem list
gem listThe 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>
gem which <FILE>Find the location of a library file you can require.
Differences from which and rbenv which:
gem info <GEMNAME>
gem info <GEMNAME>Prints information about the gem such as name, description, website, license and installed paths
gem environment
gem environmentDisplay information about the RubyGems environment.
gem environment gemdir
gem environment gemdirDisplay the path where gems are installed.
gem environment gempath
gem environment gempathDisplay path used to search for gems.
gem environment remotesources
gem environment remotesourcesdisplay the remote gem servers.
gem environment -h
gem environment -hgem -h
gem -hgem help <COMMAND>
gem help <COMMAND>You can also use
gem <COMMAND> -h
Display help information for a command. e.g.
require gem in code
require gem in codeRubyGems 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
-rtoirbwill 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 gem)
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
ri: viewing DocumentationYou can view the documentation for your installed gems with ri:
fetch and unpack gems
fetch and unpack gemsIf 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
-Iargument adds your unpacked rake to the ruby$LOAD_PATHwhich prevents RubyGems from loading the gem version (or the default version).The
-Sargument findsrakein the shell's$PATHso 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 rake 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
libdirectory contains the code for the gemThe
testorspecdirectory contains tests, depending on which test framework the developer usesA gem usually has a
Rakefile, which the rake program uses to automate tests, generate code, and perform other tasks.This gem also includes an executable file in the
bindirectory, which will be loaded into the user'sPATHwhen the gem is installed.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
gemspecThe 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