Testing The Ruby Waters
September 24, 2014 Aaron Bartell
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 also called 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 powerruby.com 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 check out my recent Four Hundred Guru 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 timesavers 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. Before starting irb it’s always good to check what Ruby version your session 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. 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. 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 the test. One feature of Ruby is parentheses on method calls, which are optional. To learn whether this is true we can call the puts method, first without parentheses and then with them. 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. 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. 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. 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. Once you are done using irb you can exit by typing “quit” and you will be returned to the bash shell, as shown below. 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. Aaron Bartell is a Rubyist at heart, though it wasn’t always that way. Going back more than a decade has Aaron loving RPG and Java as they were the primary means of creating mobile or website solutions on IBM i. That all changed when he was introduced to the Ruby language and Rails web framework because it became very apparent how much time and effort could be saved to produce excellent applications. Ruby on i is now a reality with PowerRuby, so what are you waiting for!? 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. Send your questions or comments for Aaron to Ted Holt via the IT Jungle Contact page. RELATED STORY
|