• The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
Menu
  • The Four Hundred
  • Subscribe
  • Media Kit
  • Contributors
  • About Us
  • Contact
  • A Ruby And RPG Conversation

    March 17, 2015 Aaron Bartell

    “Due diligence” and “risk assessment” are phrases that should be running through your head anytime technology decisions are being made where new tooling or ideas are being put into production. The same is true when considering whether the Ruby language has a place in your shop. After all, it is a significant change in direction when introducing a new language to your technology stack.

    What many people don’t know is the adoption of Ruby (and the Rails web framework) can be done in incremental fashion if that is what works best for you. What I mean by that is not only can you make use of your existing DB2 tables, but also your significant investment in RPG code. How? Well, that’s what this article is all about. Let’s dive in.

    The Ruby language has this concept called RubyGems, which is a package manager for ease of distributing and maintaining Ruby code (usually open source). Think of a RubyGem being similar to RPG *SRVPGMs being stored in a *SAVF for distribution to other machines, except all done based on an agreed upon specification that includes tools for downloading, installing, and updating. Or maybe better stated, imagine a world where we had so much open source RPG code that we had to come up with streamlined ways to share it. That’s RubyGems. I will cover RubyGems more thoroughly in a future article.

    Included with PowerRuby is a RubyGem named the xmlservice gem. The xmlservice gem is a native-to-Ruby way of communicating with the platform-and-language-agnostic XMLSERVICE open source project developed in IBM Rochester by IBM’er Tony Cairns. The xmlservice gem can be used to communicate with nearly any resource on IBM i including DB2 tables, data queues, and most notably an RPG program or service program.

    Below we have a very simple RPG program that receives in two parameters passed by reference, alters their values, and then returns. Note, this is the new 100 percent free-form RPG introduced in V7.1 TR7. If you are like me you might still be adjusting. I didn’t want to be told I wasn’t modern with RPG so I am trying to change my ways.

    --- MYLIB/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;
    

    Our goal is to invoke PGM1 from Ruby and display the value back to the screen. In my article Testing The Ruby Waters, I introduced the idea of testing Ruby code from irb (interactive Ruby shell) and that’s what we will do here. Below we have the entirety of the Ruby code necessary to invoke the aforementioned RPG program using the xmlservice gem. In this scenario we are making use of the database adapter for communication with the other option being the RESTful adapter (i.e., invoke the RPG program via an HTTP request).

    --- Lines of Ruby code to paste into IRB ---
    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
    

    The first four lines are Ruby require statements that are similar to doing /copy in RPG–bringing outside functionality into the program.

    Next we establish the database connection details where a specific library (a.k.a., schema) needs to be specified, though this has no bearing on where the actual RPG program resides most likely because the world of database adapters isn’t used to the concept of storing executable programs inside of what they’d consider a database schema. In short, you will need to supply an existing library for the schema value and also a valid username and password.

    Next we have the configuration of the call to PGM1 in MYLIB by calling XMLService::I_PGM.new, which then uses the left-shift operator (<<) to append the two parameters, mychar1 and mydec1. Note that a default value is also being specified for both parameters, ‘a’ and 11.1111 respectively. You could have instead used variables but I’ve hard-coded for the sake of brevity.

    At this point the variable pgm1 contains all the information it needs to invoke the RPG program. On the next line the call method does the actual invocation of RPG and waits for a response. As mentioned before, this particular scenario we are calling RPG through a DB2 connection, which is effectively a generic stored procedure–very fast.

    The last two puts lines are conveying the resulting variable values to the terminal.

    When you paste the above into an irb session it will have a lot more output conveyed back to you, as shown in this gist. What is a gist you ask? A way to easily share a snippet of code, or logs, or other text; either privately or with the world.

    The above code would be too verbose to use multiple times in an application so it would be better to encapsulate it into something like the following:

    require 'active_support'
    require 'active_record'
    require 'ibm_db'
    require 'xmlservice'
    
    class Pgm1
      attr_accessor :char1, :dec1
      def initialize
        ActiveRecord::Base.establish_connection(
          adapter: 'ibm_db', 
          database: '*LOCAL', 
          schema: 'MYLIB', 
          username: 'xxxxx', 
          password: 'xxxxx'
        )
        @char1 = nil
        @dec1 = nil
      End
      def call(c1, d1)
        pgm1 = XMLService::I_PGM.new("PGM1", 'MYLIB') <<
               XMLService::I_a.new('mychar1', 1, c1) <<
               XMLService::I_p.new('mydec1', 7, 4, d1) 
        pgm1.call
        @char1 = pgm1.response.mychar1
        @dec1 = pgm1.response.mydec1
      End
    end
    

    Then you can more simply invoke it as follows.

    p = Pgm1.new
    p.call('a', 11.1111)
    puts p.char1
    puts p.dec1
    

    Another code refinement would be to place the establish_connection portion in a separate file rather than have it in each Ruby class that represents an RPG program. As you may have guessed, we’ve officially ventured into authoring object-oriented Ruby code without me first explaining it. I did that on purpose because sometimes it’s better to have a full working example and then go back and learn the individual pieces. Here is a good one-pager to learn the object oriented aspects of Ruby.

    That wraps up this installment of Ruby learning. It is worth noting that the XMLSERVICE API is very flexible when compared to its predecessors (i.e., PCML, Java Toolbox) and the simplicity of my example is not a reflection of XMLSERVICE’s capabilities. It should also be noted that XMLSERVICE allows you to create stateful jobs on the server side so you can make subsequent calls and have things be retained (i.e., open data paths, global variables in a service program, etc.). This opens the door for Ruby and XMLSERVICE to be used for RPG unit testing–something I hope to cover in a future article. Stay tuned!

    Aaron Bartell is Director of IBM i Innovation for Krengel Technology, Inc. Aaron facilitates adoption of open source technologies on IBM i through professional services, staff training, speaking engagements, and the authoring of best practices within industry publications and www.litmis.com. With a strong background in RPG application development, Aaron covers topics that enable IBM i shops to embrace today’s leading technologies including Ruby on Rails, Node.js, Git for RPG source change management and RSpec for unit testing RPG. Aaron is a passionate advocate of vibrant technology communities and the corresponding benefits available for today’s modern application developers. Connect with Aaron via email at abartell@krengeltech.com. Aaron lives with his wife and five children in Southern Minnesota. He enjoys the vast amounts of laughter having a young family brings, along with camping and music. He believes there’s no greater purpose than to give of our life and time to help others.

    RELATED STORIES

    Git To GitHub

    Git To It

    Knee-Deep In Ruby Waters

    Testing The Ruby Waters

    Bash Is Not A Shell Game

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Tags:

    Sponsored by
    DRV Tech

    Get More Out of Your IBM i

    With soaring costs, operational data is more critical than ever. IBM shops need faster, easier ways to distribute IBM applications-based data to users more efficiently, no matter where they are.

    The Problem:

    For Users, IBM Data Can Be Difficult to Get To

    IBM Applications generate reports as spooled files, originally designed to be printed. Often those reports are packed together with so much data it makes them difficult to read. Add to that hardcopy is a pain to distribute. User-friendly formats like Excel and PDF are better, offering sorting, searching, and easy portability but getting IBM reports into these formats can be tricky without the right tools.

    The Solution:

    IBM i Reports can easily be converted to easy to read and share formats like Excel and PDF and Delivered by Email

    Converting IBM i, iSeries, and AS400 reports into Excel and PDF is now a lot easier with SpoolFlex software by DRV Tech.  If you or your users are still doing this manually, think how much time is wasted dragging and reformatting to make a report readable. How much time would be saved if they were automatically formatted correctly and delivered to one or multiple recipients.

    SpoolFlex converts spooled files to Excel and PDF, automatically emailing them, and saving copies to network shared folders. SpoolFlex converts complex reports to Excel, removing unwanted headers, splitting large reports out for individual recipients, and delivering to users whether they are at the office or working from home.

    Watch our 2-minute video and see DRV’s powerful SpoolFlex software can solve your file conversion challenges.

    Watch Video

    DRV Tech

    www.drvtech.com

    866.378.3366

    Share this:

    • Reddit
    • Facebook
    • LinkedIn
    • Twitter
    • Email

    Sponsored Links

    COMMON:  2015 Annual Meeting & Expo, April 26 - 29, at the Disneyland® Resort in Anaheim, California
    Profound Logic Software:  Extend & Future-proof RPG Apps with PHP. March 25 Webinar!
    COMMON:  2015 Annual Meeting & Expo, April 26 - 29, at the Disneyland® Resort in Anaheim, California

    Storagepipe Takes Aim At IBM i Tape Backup And Recovery IBM Patches BIND and OpenSSL Flaws in IBM i

    Leave a Reply Cancel reply

Volume 15, Number 05 -- March 17, 2015
THIS ISSUE SPONSORED BY:

ProData Computer Services
HelpSystems
WorksRight Software

Table of Contents

  • Death To Decimal Data Errors!
  • SQL Joins With Tree Structures: An Oracular Point Of View
  • A Ruby And RPG Conversation

Content archive

  • The Four Hundred
  • Four Hundred Stuff
  • Four Hundred Guru

Recent Posts

  • The Power11 Transistor Count Discrepancies Explained – Sort Of
  • Is Your IBM i HA/DR Actually Tested – Or Just Installed?
  • Big Blue Delivers IBM i Customer Requests In ACS Update
  • New DbToo SDK Hooks RPG And Db2 For i To External Services
  • IBM i PTF Guide, Volume 27, Number 33
  • Tool Aims To Streamline Git Integration For Old School IBM i Devs
  • IBM To Add Full System Replication And FlashCopy To PowerHA
  • Guru: Decoding Base64 ASCII
  • The Price Tweaking Continues For Power Systems
  • IBM i PTF Guide, Volume 27, Numbers 31 And 32

Subscribe

To get news from IT Jungle sent to your inbox every week, subscribe to our newsletter.

Pages

  • About Us
  • Contact
  • Contributors
  • Four Hundred Monitor
  • IBM i PTF Guide
  • Media Kit
  • Subscribe

Search

Copyright © 2025 IT Jungle