Knee Deep in Ruby Waters

by | Aug 3, 2017

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

In the article Testing The Ruby Waters, I introduced you to Ruby and how to use irb (the interactive Ruby shell) to easily run and test Ruby code. In this article we will expound on that and introduce more features of the Ruby programming language as it relates to an IBM i programmer – in particular, Ruby methods and one form of encapsulation.

Ruby is an object oriented language – everything in Ruby is an object. But that doesn’t inhibit Ruby from being used in a more procedural approach. Below we have a Ruby program named say_hi.rb with a method definition of hi, and then the invocation of hi. Note, Ruby methods are like subprocedures in RPG. Notice how there isn’t any sort of a class definition or any other means of encapsulating around the hi method. Ruby does have the ability to define a class, but it isn’t required, and that’s a good thing.

To invoke a Ruby program you need to pass the .rb file to the ruby runtime, as follows. Note I am in the same directory as the program in this case. If you are in a different directory you need to adequately convey the file path – full or relative. Also note I am in a shell environment which you can learn more about here.

Running say_hi.rb

Running say_hi.rb

Let’s mix things up a bit and copy/paste this test program into irb, as shown below. Note how I changed the order by first having the method invocation and then the method definition. Learn more about irb here.

undefined method 'hi'

undefined method ‘hi’

Here we learn an interesting aspect of Ruby in that when hi was called there wasn’t yet a method definition so it threw an error. Before a method can be called upon it must first be defined. This is where we can start encapsulating our code by storing the hi method definition in a separate file, as shown below. Encapsulating (aka modularizing) is a familiar concept to RPG’ers by way of *SRVPGMs and the like.

Below we see the hi method definition now exists in file talk.rb. Then the say_hi.rb file needs to have a require_relative statement to bring the talk.rb file into say_hi.rb. Think of require_relative as being similar to a /copy statement in RPG. Note, talk.rb and say_hi.rb are located in the same directory.

Now when we invoke say_hi.rb again we get the expected results of “hi Aaron” in the terminal. To take this example further it would be good to wrap our hi method in a namespace so it doesn’t conflict with any other methods named hi. To do this we can use Ruby’s module concept which is similar in nature to Java’s package concept. RPG doesn’t yet have namespaces so what I usually do is prefix the subprocedure with the name of the module (i.e. Cust_getAddress).

 

 

Below we’ve added a module statement named Talk that wraps the hi method definition. We also need to declare that the hi method belongs to the Talk module so we name it self.hi instead. Why do we need to add “self.”? That’s a bigger conversation for another day, but it has to do with what’s called “mixins”.

Then in say_hi.rb we need to qualify the call to be Talk.hi. Tada! We now have namespaced methods and modularized code!

Next let’s learn whether Ruby is passing parameters by value or reference – RPG can do both by using the VALUE keyword on a subprocedure definition. The prevailing user community documentation says Ruby is strictly pass-by-value but this isn’t entirely correct. The normal approach to proving Ruby is pass-by-value is to first assign a variable a value, call a method that changes the passed-in variable, and then display the original variable value again from the caller, as shown below.

Now when we invoke the program again we will see the value of variable x remains the same even after the hi method changes the value of the name variable. This is where most usually say “Ha! See, obviously Ruby is pass-by-reference”.

"Aaron, welcome to the jungle"

“Aaron, welcome to the jungle”

What actually happens is a Ruby method call passes an object id reference, and if the variable is modified in the method then, and only then, it will create a new object id reference for that variable so the caller’s variable value isn’t altered. This is called copy-on-write and is an optimization strategy where two or more variables can point at the same object reference until one of them needs to change.

Let’s prove the copy-on-write concept by displaying the object id for each variable at various stages throughout the method call to see when it changes.

Below are the results and we can see that as soon as the Talk.hi method altered the name variable it produced a new object id, and when control was returned to the caller that x still had the original object id.

Object IDs for the name variable

Object IDs for the name variable

From this little exercise we now know that Ruby is technically pass-by-reference but because of it’s copy-on-write approach it feels like pass-by-value. While this is definitely beyond a beginners topic I thought the distinction was important given we have both approaches in RPG, though not a copy-on-write approach. Stay tuned for more on the Ruby language in future articles.

RPG-XML Suite

Easy web service API integration from RPG

Learn More

CyberSource Toolkit for i

Process credit cards from RPG

Learn More

Follow Us

Want to learn more about Ruby? Hit us up!

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

Get Social

Share to your favorite social platform