In part 2 of our series, we learned about the Ruby community and introduced RubyGems.
Now we get to dive further into the many benefits available within gems for developers!
Note sure what RubyGems is? For review, here is the Wikipedia definition:
RubyGems is a package manager for the Ruby programming language that provides a standard format for distributing Ruby programs and libraries (in a self-contained format called a “gem”), a tool designed to easily manage the installation of gems, and a server for distributing them. RubyGems was created in November 2003 and is now part of the standard library from Ruby version 1.9.
So what type of functionality can you get your hands on via RubyGems?
The best way to find that out is to head over to The Ruby Toolbox – a web site that categorizes RubyGems for search and comparison purposes. For example, here is a link to the “Web Framework” category where we can learn not only what’s available but also get a rating of each web framework to help us make a decision on what to pursue. The ratings are based on a variety of criteria including how often new code is submitted, how many Git pull requests are outstanding, and the number of “watchers”. The Ruby Toolbox web site was necessary because of so many RubyGems available to pick from. Technology professionals needed a more efficient way of knowing what to select.
Let’s take a particular business requirement example of needing to produce a PDF based on DB2 data. By visiting The Ruby Toolbox I can learn that Prawn is probably the best pick, as shown below.
Now that we know the Prawn RubyGem exists, how easy can we make use of it? Obtaining a RubyGem is simple using a PASE shell session (i.e. either CALL QP2TERM or ssh into the machine). Below shows an example of me logging into my IBM i PASE environment via SSH. You can use the free Putty tool to accomplish this on Windows. Note you need to have the free PowerRuby installed on your machine before you can run Ruby code.
Before obtaining Prawn I want to alter the RubyGem install location by creating a “Gem Set”. Think of a Gem Set as an isolated folder where I can maintain Gems for a particular purpose. In this case I simply don’t want to install the Gems in the default location (which is the /PowerRuby folder), because when I upgrade PowerRuby it will delete those Gems.
First make a new directory for the Gems (i.e. /home/aaron/gemsets/project1). Then change PASE environment variable GEM_HOME, which is an alias for “installation directory”, to point at the newly created directory. Last set the GEM_PATH to have the installation directory first and the PowerRuby Gem location second. The GEM_PATH works similarly to a library list concerning dependency resolution where directories are checked from left to right.
Now it’s time to install the Prawn Gem with the following command:
$ git install prawn
When the above command is run it will go out to RubyGems.org and check to see if there is a Gem named “prawn”. Because we didn’t specify a version it will download the latest to our IBM i. Inside of each Gem is a .gemspec configuration file that declares a number of things, but in particular it documents dependencies on other Gems. In this case we can see the prawn gem requires ttfunk and pdf-core and it downloads and installs those first.
That was simple!
Now it’s time to test the prawn Gem to see how it works. To do that we will load the Interactive Ruby Shell, (irb for short), as shown below. The irb is great for quickly testing Ruby code. I used it a lot while learning the Ruby language.
Here is what the results look like. Note that I issued the quit command to leave irb and ls -all hello.pdf to list the newly created PDF.
I can then download hello.pdf to my laptop and view it, as shown below.
I hope this gives you a good idea of how elegant it is to make use of open source Gems!
The Ruby community has grown tremendously because of efficiencies like this. This is just one reason why the language and community is so powerful.
Give it a shot! Shoot us an email with any questions to email@example.com.
EXCITING ANNOUNCEMENT! Concerning RubyGems – we wanted to announce that the Krengeltech Litmis Team and IBM recently agreed to co-develop the open source xmlservice Gem which can be found here. This is the Gem used to seamlessly communicate from Ruby to anything on IBM i (i.e. RPG program, data queue, commands, etc). More to come on how to make use of the xmlservice Gem in a future article!