Gems in Ruby: an introduction
Gems can help us do more while programming in Ruby. Gems are self-contained programs which are easily installed and managed by the RubyGem packet manager. They cover almost all aspects of the development process. These programs allow us to build on them and allow us to do more with our code; they can reduce the need to do tedious or repetitive tasks.
RubyGems was released in 2004 and added a lot of functionality to the Ruby language. Gems in Ruby can be compared to pip in python or npm in javascript as they are both package installers but gems are packaged with an installer and package manager which makes them easy to be installed and distributed. This ease of use is what makes ruby an easy to use language. Most gems are written in ruby and anyone can make one.
To install a gem in the terminal just execute gem install mygem; mygem being then name of the gem. This allows them to be used anytime any new code is executed because RubyGem integrates with the Ruby runtime loader. Rails and its components are distributed as gems.
Many gems are available publicly at the RubyGems repository. There are over 160k gems available for download publicly from the repo. This community of developers is also what makes ruby a great language to use and develop with. Some of the most popular gems are rspec, diff-lcs, bundler, rack and rake.
Examples of Gems
Rspec is a DSL (domain specific language) used to test ruby code. It is a behavior-driven development(BDD) framework. It is very simple to use and useful tool for Ruby developers and is why it is the most popular gem in the repo.
Bundler is a gem that helps gems install dependencies that may be required to use a gem.
Rack simplifies web development by streamlining the tasks by wrapping HTTP requests and responses. Rack is the technology behind basically all of the web frameworks in the Ruby world.
Rake is a very powerful tool that helps to automate tasks while writing code for ruby repetitive tasks such as migrating tables to a database can be done with rake. Rake helps ruby developers speed up the amount of time and code they would’ve had to write.
Structure of Gems
Gems follow a simple structure that make them easy to understand. Gems usually include code, documentation, and a gemspec file. A gem is organized a lot like any other Ruby application. The lib directory contains the code for the gem and tests such as rspec are stored in the test directory. The executable file is stored in the bin directory. The aforementioned gemespec file stores information about a gem such as its name, version, and description.
% tree
.
├── Rakefile
├── bin
│ └── hola
├── hola.gemspec
├── lib
│ ├── hola
│ │ └── translator.rb
│ └── hola.rb
└── test
└── test_hola.rb#The file structure of a gem source: ruby gems guides
Gems are a very important part of the Ruby programming language and it allows Rubyists to streamline their development process in a variety of different ways. The gems discussed only scratch the surface of the vast library of gems available.