Programming often involves repetitive tasks. Most programming languages give you some way to package common code, making it reusable across multiple applications. In this article, we’ll look at how Ruby solves the problem of code reuse in a repeatable way with Ruby Gems. We’ll also take a look at managing gem sources, giving you more options to install additional functionality libraries from various third parties.
- Ruby version – 2.1.2 or newer
Expanding Your App’s Capabilities
As your software code base grows more complex, you may find that you have to write the same code multiple times. This repetition is obviously inefficient since now any changes to that code need to occur in multiple places when you introduce a new feature or a bug fix. You can mitigate this issue by encapsulating the code into separate classes–which solves the reuse problem–but this solution only scales to a certain point. Once you start trying to add complex functionality–such as HTTP communication between different applications–you will often find yourself working in well-defined problem domains that have well-known solutions. So while it might be a good learning exercise to write your own solution to one of these problems, ultimately your application would be best served by using one of these pre-existing solutions.
In Ruby, we address this issue by using third-party libraries known as Gems. Ruby Gems are sets of code that implement common functionality. Using a third-party library simply requires downloading the code and putting it into a common location accessible from any program running on your computer.
Basic Gem Management Commands
We’ll start with some of the more commonly used
Adding New Gems
gem install command works in a similar fashion to package management installers in Unix or Unix-like environments.
gem install XXXX
This command uses the RubyGems application to access your installed gem sources and locate a gem named
XXXX. RubyGems downloads the gem and installs it into your current Ruby version’s common directory. Then, all of the Ruby applications in your development environment that use the current version of Ruby will have access to the new gem’s functionality.
Checking Local Gem Installs
Figuring out which gems are currently installed on your machine can be daunting, and in a worst-case scenario would involve navigating to the RVM install directory for your current version. Luckily, we have access to
gem list. The default command lists all gems installed in your current environment.
You can also search for a specific gem by appending either its name or a regular expression to the command:
gem list NAME/REGEX
Finally, you can obtain extra information (including gem authors, version, appropriate license, and install directory) by adding the
gem list -d
Adding a New Gem Source
Many of the most popular gems are accessible from rubygems.org, the default gem source for Ruby. To add a new gem source:
gem sources -a URL
-a flag indicates that we will be appending a new gem source, and the
URL that follows is the web address of the gem server you wish to add.
Managing Gem Sources
Sometimes you want to manage your source list–typically when a gem’s source changes locations. Ruby Gems gives you several options for managing your gem source list.
To list all gem sources, use the
gem sources -l
To remove a gem source, use the
r flag followed by the URL of the source to be removed.
gem sources -r URL
You can find a complete reference for the
gem sources command, along with other gem-related commands, at rubygems.org
Other Helpful Gem Commands
For most day-to-day work, you’ll most often only need the above
gem commands. There are many others, though, so we’ll present a few interesting examples.
Uninstalling a Gem
Sometimes a gem outlives its usefulness, or it is superseded by a superior alternative. In those cases, you might want to uninstall these gems to prevent too much unused cruft from building up in your Ruby install. To uninstall a gem, use the following command (where
GEMNAME is the name of the gem to uninstall):
gem uninstall GEMNAME
Updating Installed Gems
Gems are often under active development by the community, which makes it important to keep your local gemset up-to-date. To update all gems in your system, use the
gem update command.
You can also update a specific gem by providing a regular expression that matches gem names (replacing
NAME below with your regular expression):
gem update NAME
Getting Information on Available Gems
Searching for gems that may already be available in your gem sources can be tedious if using the web interfaces available (such as that at rubygems.org). To get around this chore, we can use
gem query. This comamnd queries all configured gem repositories on your machine for additional information, and is the quickest way to tell whether or not you have access to a given gem. To see if a specific gem is present in your local and remote sources, use the following command:
gem query -b NAME
In place of
NAME substitute either the name of a gem or a suitable regular expression. The
-b flag indicates that you want to check both local and remote sources. To check either local or remote sources, use the
-r flags, respectively.
Many gem developers will include comments compatible with automatic documentation generators such as RDoc. To generate RDoc documentation for an installed gem, use the following command (replacing
NAME with the name of the gem):
gem rdoc NAME
Additionally, you can generate RDoc documentation for all installed gems with the
--all flag (note: this will likely take a long time to complete).
gem rdoc --all
Finding New Gems
There are two typical places that Ruby developers find gems:
- Recommendations from the Ruby community. Often another Ruby developer will write a blog post about a specific set of functionality, or a developer will ask a question on a forum (such as Stack Overflow) about how to overcome a specific problem using Ruby.
- Searches on Ruby Gems repository or other repositories. These repos tend to offer stats on a particular library, such as a brief statement of a gem’s purpose, a gauge of the gem’s general popularity, and a link to the original host of the gem.
Once you do locate a gem, you may also have to evaluate its effectiveness and trustworthiness. Of course, with open-source Ruby gems, analyzing the code to ensure nothing strange is taking place is a straightforward matter of looking at the gem’s Github repo. Using a gem repository’s reporting functionality can solve the trustworthiness problem, but it’s not perfect. For example, a brand-new gem that solves a problem in the most efficient way possible may be falsely reported as being inferior to another more-popular-but-older gem that solves the problem in a less efficient manner.
Through judicious use of RubyGems, you can greatly reduce the time required to develop Ruby applications by leveraging the expertise of other developers from around the world. You can then ensure that your code operates with a common interface that matches community development standards.
Thanks for following along with this how to: Adding Ruby Gems and Gem Sources. Learn more about our affordable VPS hosting solutions and be sure to check back with us again for updates, tutorials and useful information like What Is RVM (Ruby enVironment Manager or Ruby Version Manager)?