[et_pb_section bb_built=”1″ fullwidth=”on” specialty=”off” _builder_version=”3.0.64″ next_background_color=”#000000″][et_pb_fullwidth_post_title admin_label=”Blog Post Header” _builder_version=”3.17.2″ title=”off” meta=”off” categories=”off” comments=”off” featured_placement=”background” text_orientation=”center” module_class=”blog_post_header” title_font_size=”60px” background_blend=”saturation” title_font=”Raleway|on|||” title_text_color=”#274060″ title_letter_spacing=”2px” meta_font=”Raleway||||” meta_font_size=”20″ meta_text_color=”#274060″ meta_letter_spacing=”2px” /][/et_pb_section][et_pb_section bb_built=”1″ _builder_version=”3.0.63″ prev_background_color=”#000000″][et_pb_row _builder_version=”3.0.63″][et_pb_column type=”4_4″][et_pb_text admin_label=”Blog Post” _builder_version=”3.0.69″ global_module=”30698″ saved_tabs=”all” background_layout=”light” text_orientation=”left” border_style=”solid”]

Originally published: MCPress Online on 2/05/2015
Updated author notes: This article includes reference to the ruby-itoolkit tool which is now open source and hosted on Bitbucket.

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 InteractiveRuby 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.

In the irb session, define a method and invoke it.
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.

[/et_pb_text][/et_pb_column][/et_pb_row][et_pb_row _builder_version=”3.0.47″ background_size=”initial” background_position=”top_left” background_repeat=”repeat”][et_pb_column type=”4_4″][et_pb_text admin_label=”Contact Form Title” background_layout=”dark” _builder_version=”3.0.69″ text_font=”Raleway|on|||” text_font_size=”26″ text_font_size_tablet=”26″ text_font_size_phone=”26″ text_text_color=”#f45d01″ text_letter_spacing=”2px” text_letter_spacing_tablet=”2px” text_letter_spacing_phone=”2px” text_line_height_tablet=”1.7em” text_line_height_phone=”1.7em” header_font=”||||” header_font_size_tablet=”30px” header_font_size_phone=”30px” header_letter_spacing_tablet=”0px” header_letter_spacing_phone=”0px” header_line_height_tablet=”1em” header_line_height_phone=”1em” global_module=”30700″ saved_tabs=”all”]

Need Ruby to talk to RPG? We can help. Contact us below.

[/et_pb_text][/et_pb_column][/et_pb_row][et_pb_row admin_label=”Litmis Article Footer” background_position_1=”top_left” background_repeat_1=”no-repeat” _builder_version=”3.0.62″][et_pb_column type=”4_4″][et_pb_text admin_label=”Contact Form” _builder_version=”3.0.64″ global_module=”30748″ saved_tabs=”all”]

  • This field is for validation purposes and should be left unchanged.

[/et_pb_text][/et_pb_column][/et_pb_row][/et_pb_section]

Leave a Reply

Your email address will not be published. Required fields are marked *