RubyGems

What are RubyGems?

Emily Joy
4 min readJul 5, 2022

--

Have you ever looked at the Gemfile in a Ruby project and wondered what all those gems are doing in the background and how they work? RubyGems (just like node_modules in React) are software packages that you can download to your local machine and use in your Ruby projects. Each package or “gem” contains specific features to make building your Ruby project easier and more comprehensive! One of the main reasons gems were created is to provide reusable effects and functions for all Rubyists to utilize. Some gems even have the ability to automate tasks in the command line, helping you work more efficiently.

Thanks to the hard work of our fellow Rubyists, there is an extensive library of gems available for us in Ruby, and you can even create your own custom gem and publish it for the community (be sure to search the RubyGems guides and google prior to creating your own gem — you would be surprised how many exist already!).

So what exactly is a gem? According to the RubyGems Guide :

“The RubyGems software allows you to easily download, install, and use ruby software packages on your system. The software package is called a “gem” which contains a packaged Ruby application or library.”

Each gem has a name, version, and specific platform it can function on. The structure of a gem contains code(which has tests and supporting utilities), Documentation, and a gemspec. The gemspec has basic information such as name, version, description, and author(s) of the gem.

If you are using Ruby version 1.9 or newer, RubyGems functionality are automatically provided to you. To check your Ruby version, run the following in your command line :
$ ruby --version

To check your RubyGems version, run:
$ gem --version

To update to the latest RubyGems version, run:
$ gem update --system

If you are having any issues installing RubyGems or updating to the latest version you can run :
$ ruby setup.rb--help

You can search for gems in the command line by name. For example if you wanted to find all the gems associated to sinatra, you could type the below into your terminal for a complete list of gems with Sinatra in their name :

$ gem search ^sinatra

*** REMOTE GEMS ***

sinatra (2.2.0)
sinatra-accept-params (0.1.0)
sinatra-accept-params-d1plo1d (0.1.1)
sinatra-acd (1.4.5)
sinatra-ace (0.0.0)
sinatra-activerecord (2.0.25)
[...]

Hmmm, I wonder what the sinatra-activerecord gem does? Lets get some details on the gem using the -d command!

$ gem search ^sinatra-activerecord

*** REMOTE GEMS ***

sinatra-activerecord (2.0.25)
Authors: Blake Mizerany, Janko Marohnic, Axel Kee
Homepage:
http://github.com/sinatra-activerecord/sinatra-activerecord
Extends Sinatra with ActiveRecord helpers.

The detail provides us a link to the github repo which lists descriptions, requirements, and other useful information, in the README file :

“Extends Sinatra with extension methods and Rake tasks for dealing with an SQL database using the ActiveRecord ORM.”

We want to create a database using SQL, so lets install the gem using the gem install command :

$ gem install sinatra-activerecord
Fetching rack-2.2.4.gem
Successfully installed rack-2.2.4
Successfully installed sinatra-activerecord-2.0.25
Parsing documentation for rack-2.2.4
Installing ri documentation for rack-2.2.4
Parsing documentation for sinatra-activerecord-2.0.25
Installing ri documentation for sinatra-activerecord-2.0.25
Done installing documentation for rack, sinatra-activerecord after 4 seconds
2 gems installed

When we installed sinatra-activerecord, it also installed any needed dependencies and created the documentation about the gem so we have information on how the gem functions.

To view the documentation on a gem through the terminal, use the command ri :

$ ri pry
= .pry
(from gem pry-0.14.1)
=== Implementation from Object
------------------------------------------------------------------------
pry(object = nil, hash = {})
------------------------------------------------------------------------Start a Pry REPL on self.If `self` is a Binding then that will be used to evaluate expressions;
otherwise a new binding will be created.
@param [Object] object the object or binding to pry
(__deprecated__, use `object.pry`)
@param [Hash] hash the options hash @example With a binding
binding.pry
@example On any object
"dummy".pry
@example With options
def my_method
binding.pry :quiet => true
end
my_method()
@see Pry.start(from gem pry-0.14.1)
=== Implementation from Pry::REPL
------------------------------------------------------------------------
@return [Pry] The instance of {Pry} that the user is controlling.(from gem pry-0.14.1)
=== Implementation from Pry::Testable::PryTester
-----------------------------------------------------
-------------------
(END)

We can view all the gems installed on our local machine by using the gem list command :

$ gem list*** LOCAL GEMS ***activemodel (6.1.4.1, 6.1.4, 6.1.0)
activerecord (6.1.4.1, 6.1.4, 6.1.0)
activesupport (6.1.6, 6.1.4.1, 6.1.4, 6.1.0)
addressable (2.3.8)
awesome_print (1.9.2)
[...]

And if you installed a gem you don’t need by accident, you can uninstall it by using the gem uninstall command :

$ gem uninstall thorYou have requested to uninstall the gem:
thor-1.2.1
learn-co-4.1.0 depends on thor (>= 0.19.1)
If you remove this gem, these dependencies will not be met.
Continue with Uninstall? [yN] y
Remove executables:
thor
in addition to the gem? [Yn] y
Removing thor
Successfully uninstalled thor-1.2.1

There is extensive documentation on RubyGems at the below link, including step by step instructions on creating your own unique RubyGem from scratch! Sounds like a great topic for my next blogpost! Thanks for reading, Happy {simpler} coding!

References

--

--