Back-end Engineering Articles

I write and talk about backend stuff like Ruby, Ruby On Rails, Databases, Testing, Architecture / Infrastructure / System Design, Cloud, DevOps, Backgroud Jobs, some JS stuff and more...

Github:
/danielmoralesp
Twitter:
@danielmpbp

2024-07-30

Interactive Ruby Console or IRB

8- Interactive Ruby Console or IRB

Interactive Ruby Console or IRB in Ruby is a terminal where you can test rapid and easy Ruby code. It was made thinking about debugging, playing around with ruby and doing tests rapidly with any Ruby command inside this console. 

You have 2 main ways to access a Ruby Interactive console, the first one is through a cloud service who runs IRB behind the scenes. The second option is installing IRB on your machine (local or virtual). Actually, in previous blog posts we talked about How to install Ruby in Linux and How to Install Ruby in Windows. If you’ve been following these blog posts, you have installed IRB, because it comes with RVM and with the RubyInstaller Devkit. 

But before we can understand IRB we have to know about REPL. REPL is an interactive toplevel or language shell. REPL is a simple interactive computer programming environment that takes single user inputs, executes them, and returns the result to the user; a program written in a REPL environment is executed piecewise. In a REPL, the user enters one or more expressions (rather than an entire compilation unit) and the REPL evaluates them and displays the results. The name read–eval–print loop comes from the names of the Lisp primitive functions which implement this functionality:
  • - The read function accepts an expression from the user, and parses it into a data structure in memory. For instance, the user may enter the s-expression (+ 1 2 3), which is parsed into a linked list containing four data elements.
  • - The eval function takes this internal data structure and evaluates it. In Lisp, evaluating an s-expression beginning with the name of a function means calling that function on the arguments that make up the rest of the expression. So the function + is called on the arguments 1 2 3, yielding the result 6.
  • - The print function takes the result yielded by eval, and prints it out to the user. If it is a complex expression, it may be pretty-printed to make it easier to understand.

Please refer to this article for more details about REPL

- I know, this is a lot of new jargon, but in plain english, the key concepts we have to take away are:
  • - User or developer is the one who need to enter some expressions inside the REPL
  • - REPL only lives in memory, so one you shut down the REPL, the data disappear
  • - REPL acts like an evaluator of the expressions given by the developer and shows a result or an error, so for that reason some developers call it: an interpreter
  • - It is possible to build a REPL for almost any programming language, and so for pretty much every popular language, there is one that exists. However not all languages are distributed originally with a REPL
  • - There are cloud REPLs and there are REPLs that come with your favorite language as a  default, like in our case with Ruby (IRB).


Cloud REPL platforms

Now, we can talk about this first solution: Cloud REPL platforms. The first and most well known cloud REPL platform is called Repl.it. Replit is an online IDE (integrated development environment). The advantages of cloud environments is that you don’t need to install anything locally, but in most cases you’ve to create an account. 

This kind of cloud environment supports almost any modern programming languages, so if you want to test Scala or Elixir, you don’t have to install anything until you’re pretty sure that you want to give a real chance to this new programming language. There are other platforms like: https://codeanywhere.com/, https://codesandbox.io/ among others


REPL by programming language

As we mentioned before, almost each modern programming language is distributed with their own REPL. So, the second way to access a Ruby REPL is using an Interactive Ruby Console via IRB in your local (or virtual) environment. 

Previously we mentioned that we’ve created Linux and Windows AWS instances to install Ruby, but we can access the REPL in the same way for both OS’s and also the same if you have it installed in your local machine or if you want to use Repl.it instead. Let's see how to do it now:

1- Accessing from Repl.it

Create your account and once you’re inside it, click on the blue Create button at the left side bar. It will prompt you a modal window where you can choose Ruby as Template and give it a name, and then Create Repl


Now, you’ll see the Interactive platform with a big green “Run” button at the top bar. Because you’ve  already a “Hello World” string, you just need to click that green button

After that you’ll see the output of the Repl



1- Accessing the AWS Linux instance:

Now let’s switch to our AWS Linux instance. In your console log in into the virtual machine and the access to the IRB like so:

$ ssh -p 22 -i ~/.ssh/ubuntu-webserver-rails.pem [email protected]
$ ruby -v
ruby 3.0.3p157 (2021-11-24 revision 3fb7d2cadc) [x86_64-linux]
$ irb
3.0.3 :001 > 

Now, you’re inside the IRB

2- Accessing the AWS Windows instance
Now let’s switch to our AWS Windows instance. Go first to your Amazon Lightsail instance and access the RDP. If you have a Windows local machine just skip this first step

Then, look for the CMD inside the RDP using the search bar


Finally enter the very same commands as Linux

$ ruby -v
$ irb


Now, you’re inside the IRB

Testing Ruby inside IRB

For all the 3 ways we used before, any of the next commands will work. In my case I’ll focus on the AWS Linux Instance

1- First steps in IRB

The first steps as with any new programming language you're learning, we can test different data types like strings, integers, floats or arrays

3.0.3 :001 > 1+3
 => 4 
3.0.3 :002 > 1.2 * 3.5
 => 4.2 
3.0.3 :003 > puts "Hi from IRB"
Hi from IRB
 => nil 
3.0.3 :004 > [1, 3, 5, 7]
 => [1, 3, 5, 7] 
3.0.3 :005 > arr = [1, 3, 5, 7]
 => [1, 3, 5, 7] 
3.0.3 :006 > arr
 => [1, 3, 5, 7]

My screen shot


If you don’t understand much of this command, don’t panic, we’ll see all of this in new blog posts

What we need to check now are simple things like:
  • The symbol + sums up two integers
  • The symbol * multiples two floats
  • The reserved word “puts” in Ruby prints a string
  • The character [ and ] open and close an array (data type in Ruby)
  • arr = saves the array in a variable and then we print that variable

As I mentioned, all of this terms will see it in next posts

Now the question is what is the first part of the console?

3.0.3 :001 >
3.0.3 :002 >
3.0.3 :003 >
3.0.3 :004 >
3.0.3 :005 >
3.0.3 :006 >

This is called “prompt”. A prompt is text or symbols used to represent the system's readiness to perform the next command. A prompt may also be a text representation of where the user is currently. Probably you have a different prompt compared with me, but in my case this is the means

First line of code


What does it mean?


Second line of code



What does it mean?



As you can see, once we know the meaning of each line it will become easier to create, debug and test Ruby code inside the IRB console. 

I hope you’ve learned a lot with this post. I’ll see you in the next one

Thanks for your reading
DanielM