Gems#

Gem, in general, is a Ruby software package with standardized format. You may consider it as an additional software library, which gives an extention to standard Ruby package. Most of the Gems are kept in the RubyGems.org hosting service: https://rubygems.org. They are created by the community and everyone can build and post his own Gem. Of course, it is not mandatory to use the RubyGems.org, but it is the most convinient way to extend the Ruby.

With Ruby it comes another command line tool - gem - “a sophisticated package manager for the Ruby programming language”. Like the other software package managers it allows to search, install and uninstall and create your own Gems.

To see installed packaged, use gem list. You can see a list of Gems and their versions installed on the current Ruby version. If you are using rbenv or RVM, this list depends on which Ruby installation are you currently using!

$ gem list

*** LOCAL GEMS ***

bigdecimal (1.2.0)
io-console (0.4.2)
json (1.7.7)
minitest (4.3.2)
psych (2.0.0)
rake (0.9.6)
rdoc (4.0.0)
test-unit (2.0.0.0)

Searching for Gems#

To find the required Gem visit https://rubygems.org or issue the gem search –remote command. By default this will search for all gems beggining with a string you gave, so gem search –remote crypt will find crypt (2.0) as well as cryptor (0.0.0). To see more information on found Gems, add –details switch (but this could take more time, as it must download the information for all found packages). gem search allows standard wildcards like: * - any characters, ^ - begin of the string, $ - end of the string.
Example: we are looking for a gem to help with ROT13 (Caesar Cipher) string encryption. Let’s check all gems ending with ‘rot13’ string:

 gem search --remote --details *rot13$

*** REMOTE GEMS ***

crypt-rot13 (1.0.5)
    Author: Daniel J. Berger
    Homepage: http://www.rubyforge.org/projects/shards

    Character rotation encryption, i.e. Caesar Cipher

rot13 (0.1.3)
    Author: James Robertson
    Homepage: https://github.com/jrobertson/rot13

    ROT13 is a simple letter substitution cipher; see
    http://en.wikipedia.org/wiki/ROT13

Installing Gems#

Installing Gems is like installing the other software packages - with gem install command. This solves the dependencies, downloads the required package and installs it - everything with this one command. It installs the documentation for fetched Gems as well.

$ gem install rot13
Fetching: rot13-0.1.3.gem (100%)
Successfully installed rot13-0.1.3
Parsing documentation for rot13-0.1.3
Installing ri documentation for rot13-0.1.3
1 gem installed

Using Gems#

Now you can use freshly installed Gem. There is no need to know where the package is installed, Gems are installed in the well-known path, under the current Ruby version. You can load the freshly installed gem with require ‘rot13’ statement and use it - in the example below run Rot13#rotate method:

irb(main):001:0> require 'rot13'
=> true
irb(main):003:0> Rot13.rotate('Rkgerznyl hfrshy ba Hfrarg')
=> "Extremaly useful on Usenet"

Uninstalling Gems#

To uninstall the gem, run gem uninstall command. If you have more than one version of the Gem, you must specify which one you want to delete using the –version switch or use the interactive mode:

$ gem uninstall rot13

Select gem to uninstall:
 1. rot13-0.1.2
 2. rot13-0.1.3
 3. All versions
> 3
Successfully uninstalled rot13-0.1.2
Successfully uninstalled rot13-0.1.3