Gems
Learn what gems are and how to use them in Ruby.
We'll cover the following
What are gems?
Until now, all of our experiments have been quite straightforward. However, with two-dimensional arrays, we may have noticed the lack of visual cues. For example, the array for the “Sea Battle” game doesn’t look very intuitive in the console:
$ irb
> Array.new(10) { Array.new(10) }
=> [[nil, nil, nil, nil, nil, nil, nil, nil, nil, nil], [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil], [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil], [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil], [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil], [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil], [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil], [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil], [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil], [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil]]
We still can understand what it is, but can we quickly find out the fifth row and second column? It would help if we looked closely enough.
Ruby language designers knew that it was impossible to create something that everyone would like. Instead of having a fixed set of tools, it was decided that they would introduce extensions so that everyone could add something new to the language.
Software developers from around the world took advantage of this opportunity and built an amazing ecosystem of extensions: gems. You may have already heard about gems, especially if you know how to write code in other languages, where they are also called libraries or packets. They work similarly: for example, there is a gem
shell command in Ruby and a npm
(Node packet manager) shell command in Node.js.
The word “gem” sounds more Ruby-like than the word “packet,” but the meaning is the same for both: something that we can install and reuse if we know its name. There are a few ways of installing gems. We’ll use the gem
shell command for now, which is a part of the default Ruby language toolset, along with other commands like irb
or ruby
.
Let’s install our first gem:
$ gem install cowsay
The cowsay
gem prints a cow with a text bubble. Find the gem documentation by searching the internet for “cowsay gem documentation.” This gem will add a cowsay
command to our shell, and we should be able to run it:
$ cowsay 'Hey, Joe!'
___________
| Hey, Joe! |
-----------
\ ^__^
\ (oo)\_______
(__)\ )\/\
||----w |
|| ||
Try these commands in the terminal below.
There are lots of gems for every occasion. That’s what the Ruby language is famous for: whatever we do, there is a high chance that there will be a gem for that.
Gem doesn’t necessarily add a shell command. Often, gems are reusable pieces of code that we can attach to our program by using the require
keyword with the gem name as a parameter, such as require "something"
.
For the following lessons, we need to install one useful gem that we know we will love. This gem is quite popular and kind of an unofficial standard in the Ruby ecosystem. This happens often: independent developers make gems, and the crowds love them so much that they become an industry standard. And, sometimes, functionality from popular gems go into the Ruby language itself!
Our gem name is “pry”. There is a page on GitHub for it. Remember to always look at official documentation before installing anything: independent developers from all over the world work on gems, and the lifecycle of some gems can be unpredictable. It can be obsolete, deprecated, or just abandoned. We should always know what we install and what we will use in our project.
Here is what official documents say about pry
: “An irb
alternative and runtime developer console.” We’ll switch from the tool we’re already familiar with, irb
, to the new one. Let’s do it now and see why it’s better:
$ gem install pry
...
$ pry
>
Well, the command prompt looks similar. But let’s type an array definition in pry
and see what happens:
$ pry
> arr = [[0, 0, 1], [nil, 0, nil], [1, nil, 1]]
Try the command given above in the terminal below.
Get hands-on with 1400+ tech skills courses.