25
Wed, Dec
0 New Articles

TechTip: Trying on a Ruby Ring

General
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

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.

 

020615BartellFigure1 irb

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.

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: