Ruby is a great language and is now supported on IBM i, but is it right for you? The best way to find out is to try on this Ruby ring.
First, I want to thank MC Press Online for having me back writing again. The last article I wrote goes back to 2007; it's been too long, and I am humbled to be writing in the same publication as other great authors.
In the fall of 2013, PowerRuby became a formal port of the Ruby language and Rails web framework for IBM i, and in the spring of 2014, David Shirey introduced you to Ruby, giving an overview of history and comparison with other languages. This article will build on that foundation and dive into some tangible things you can try on your IBM i as a means to, as they say, "kick the tires"—or maybe better stated: let's try on this Ruby ring and see how it fits.
If you haven't already, head over to PowerRuby.com to download and install the free port of Ruby. If you're unable to install PowerRuby, another option is to sign up for a free Cloud9 account, a browser-based IDE where you can do all of your Ruby work.
The first thing I will introduce is the Interactive Ruby Shell, or irb for short. This environment runs from a PASE shell and allows the entering of source code that is invoked and provides an immediate response, what the programming community calls a read-eval-print loop (REPL). You can start a PASE shell session using CALL QP2TERM, but I would instead highly recommend you ssh in from your laptop with a better client, like this Chrome plugin.
Below are a few lines of Ruby code to paste into the irb session. This is some procedural Ruby code that first defines a method named say_hi and subsequently invokes it. A Ruby method is similar to a subprocedure in RPG.
def say_hi name
puts "hi #{name}"
end
say_hi "Aaron"
Figure 1 below show what it looks like to copy and paste into irb. The things entered in this irb session remain defined until you type quit. This means I can call say_hi a second time with a different name.
Figure 1: In the irb session, define a method and invoke it.
An important consideration in adopting any new language is its ability to call upon your existing investment in IBM i and RPG. That's where a RubyGem named xmlservice comes into play. A RubyGem (Gem for short) is a collection of code that focuses on accomplishing a specific task. In the following scenario, the Gem's purpose is to invoke an RPG program from the Ruby language. This Gem is jointly maintained by IBM and the Litmis.com team, which I'm on.
Below is a simple RPG program that has two parameters passed by reference. Note I am using 100% free-form RPG. This is not the RPG of your grandfather. Compile the below RPG into an object named PGM1.
dcl-pr pgm1 extpgm;
char1 char(1);
dec1 packed(7:4);
end-pr;
dcl-pi pgm1;
char1 char(1);
dec1 packed(7:4);
end-pi;
char1 = 'C';
dec1 = 321.1234;
return;
The code below is the Ruby code necessary to invoke the above RPG program. The require statements are similar to /copy in RPG; they bring in outside functionality. The next portion is a qualified method call that will establish a connection to DB2 for i. Note that you need to modify the schema (aka library), username, and password values. Also note that this can be pasted into irb for quick and easy testing.
The code then defines the pgm1 variable with both the parameter list definition and values for the call to PGM1. Line pgm1.call is doing the actual invocation of RPG. Behind the scenes, a call to a generic DB2 stored procedure is made, and that in turn invokes MYLIB/PGM1. When control returns to the Ruby program, the results are now stored in the pgm1 Ruby variable and can be accessed with the syntax pgm1.response.mychar1, as shown. The puts method stands for "put string" and will output the resulting value to the screen.
require 'active_support'
require 'active_record'
require 'ibm_db'
require 'xmlservice'
ActiveRecord::Base.establish_connection(
adapter: 'ibm_db',
database: '*LOCAL',
schema: 'MYLIB',
username: 'xxxxx',
password: 'xxxxx'
)
pgm1 = XMLService::I_PGM.new("PGM1", 'MYLIB') <<
XMLService::I_a.new('mychar1', 1, 'a') <<
XMLService::I_p.new('mydec1', 7, 4, 11.1111)
pgm1.call
puts pgm1.response.mychar1
puts pgm1.response.mydec1
Well, how did the Ruby ring fit? Obviously, there's a certain amount of new syntax to learn, but I hope this gives you an idea of how simple it is to embrace Ruby. If you have questions or issues, please feel free to comment below or go to my bio to get my email. If you have questions specific to the xmlservice Gem, please direct them to the formal Bitbucket repository.
LATEST COMMENTS
MC Press Online