[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” background_position=”top_left” /][/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.65″ background_position_1=”top_left” background_repeat_1=”no-repeat” background_position_2=”top_left” background_repeat_2=”no-repeat” module_id_2=”stickysidebar”][et_pb_column type=”4_4″][et_pb_text admin_label=”Blog Post” _builder_version=”3.0.69″ background_layout=”light” text_orientation=”left” border_style=”solid” saved_tabs=”all” global_module=”30698″]

Originally published: IT Jungle on 9/24/14
Updated author notes: PowerRuby has produced more recent versions that this article conveys.
Best to download the latest from PowerRuby.

As of October 2013 Ruby is supported on IBM i via PowerRuby. This is good news as Ruby has become a very popular programming language for producing web applications because of its simplicity, flexibility, and vast community. In this article we will be introducing you to tools that aid in learning Ruby on IBM i. Often times the easiest way to learn a programming language is by starting small and playing with it – Ruby makes this very easy to do with its included Interactive Ruby Shell, or irb for short.

The irb environment is what’s called a REPL (Read, Evaluate, Print, Loop) because you can enter one or many Ruby statements and have them invoked immediately before your eyes – no compiling nor saving to an IFS file necessary. This makes the iterative testing of code, for learning or debugging purposes, incredibly efficient.

If you haven’t already, head on over to Power Ruby to download your free copy of PowerRuby so you can follow along.

Ruby runs in PASE, and for the purposes of this tutorial we will be accessing the IBM i via SSH session and be placed into a bash shell. If these terms are new to you then please read this article.

 

 

To start an irb session you first need to be in a PASE shell.  IBM i ships with the Korn shell which can be started by typing CALL QP2TERM from a 5250 terminal. The alternative to QP2TERM is using SSH from your laptop to a bash shell on IBM i where you will get time-savers like tab-completion and command history with up/down arrows.

Below we show logging into IBM i and determining where the irb tool is using the which command. If the command isn’t found it will convey a message stating that fact and let you know which folders it looked in.

Using the which command to find irb
Using the which command to find irb

Before starting irb it’s always good to check what Ruby version your sessions is using by running ruby -v, as shown below. You can learn more about the changes in each Ruby version from the ruby-lang.org/en/news page.

Checking ruby version with ruby -v
Checking ruby version with ruby -v

If you have multiple versions of Ruby on your machine then you can switch your bash session to a different one by running the following export command. Here we are setting the PATH environment variable to have a new entry at the beginning and then append the existing contents of PATH (i.e. $PATH). Think of PATH as being similar to a library list – it is what PASE programs will look at to resolve dependencies. Note that as of this writing only Ruby version 2.0 is supported since version 1.9 is being end-of-life’d in February of 2015.

export PATH=/PowerRuby/prV1R9/bin:$PATH

Now we are ready to start the interactive Ruby shell by typing irb in bash, as shown below. As you can see we are presented with a new prompt and this is where we can start typing any Ruby syntax.

Starting irb
Starting irb

As I mentioned in the introduction, irb is a great way to learn Ruby because it makes it very quick and easy to test different statements, so let’s put that thought to test. One feature of Ruby is parentheses on method calls are optional. To learn whether this is true we can call the puts method, first without parentheses and then with.

Using puts to demonstrate optional parentheses
Using puts to demonstrate optional parentheses

We can now see it worked both ways and have a better understanding of calling Ruby methods with and without parentheses. You might be wondering what the displayed “=> nil” value means. In short, every method in Ruby returns a value and if you don’t specify one then nil is returned. In the case of puts it only sends the input parameter to standard output and doesn’t return anything else, so nil is returned and displayed.

Another aspect of Ruby we can test is whether everything is an object, or more specifically that Ruby doesn’t have native data types as other languages do. Using the .class method we can determine what class an object comes from. In the below screenshot we learn the number 20 is actually a Fixnum class and the false Ruby-reserved-word is based on FalseClass. In other languages like Java and RPG both of these would be native data types that have no underlying objects.

Demonstrating that everything in Ruby is an object
Demonstrating that everything in Ruby is an object

A next question might be “what difference does it make if a number is an object?” To answer that question it would be good to learn what capabilities Fixnum has by typing 20.methods, as shown below.

Methods available to the Fixnum class
Methods available to the Fixnum class

Wow! That’s a lot of functionality. One that looks intriguing is the between? method, so let’s try and invoke it. Note that question marks are valid characters to have in method names and declare to the programmer a boolean is the expected return value.

Testing the between? method
Testing the between? method

As we can see, when no arguments are passed it errors and tells us how many it is expecting (or we can look at the documentation). Let’s call it again with two numbers – remember, parentheses on method calls are optional. By doing this test we also learn the between? method is inclusive of the number itself. In this case we are using a literal number but a more likely situation would be a variable holding a number.

Testing the between? method with parameters
Testing the between? method with parameters

Once you are done using irb you can exit by typing quit and you will be returned to the bash shell, as shown below.

Exitting irb with the quit command
Exitting irb with the quit command

If you aren’t allowed to load PowerRuby on your machine then you can head over to tryruby.org to walk through tutorials using an irb-like tool completely from the browser. If you are a little more adventurous you can download a Ruby runtime onto your laptop – Ruby runs on most any platform. Also, if you are already versed in another language like C/C++, Java, Perl, PHP, or Python then check out this comparison page to see what’s different.

I hope this tutorial has been informative and shows how simple it is to test the waters with Ruby on IBM i. I will leave you with something additional to test: determine whether string interpolation in Ruby works for both single and double quoted strings. Remember, Google is your friend.

[/et_pb_text][/et_pb_column][/et_pb_row][et_pb_row _builder_version=”3.0.65″ background_position_1=”top_left” background_repeat_1=”no-repeat” module_id_1=”stickystop”][et_pb_column type=”4_4″][et_pb_text admin_label=”Contact Form Title” _builder_version=”3.0.64″ text_orientation=”left” border_style=”solid” saved_tabs=”all” text_font=”Raleway|on|||” text_text_color=”#f45d01″ text_font_size=”26″ text_letter_spacing=”2px” global_module=”30700″ background_layout=”light”]

Need help getting started with Ruby on IBM i? Give us a shout.

[/et_pb_text][/et_pb_column][/et_pb_row][et_pb_row admin_label=”Litmis Article Footer” _builder_version=”3.0.62″ background_position_1=”top_left” background_repeat_1=”no-repeat”][et_pb_column type=”4_4″][et_pb_text admin_label=”Contact Form” _builder_version=”3.0.64″ background_layout=”light” text_orientation=”left” border_style=”solid” saved_tabs=”all” global_module=”30721″]

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

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

2 Responses

Leave a Reply

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