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, and more...



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: a cloud service that runs IRB behind the scenes. The second option is installing IRB in your machine (local or virtual). In previous blog posts, we've been talking about Installing Ruby in Linux and Installing Ruby in Windows. If you've been following these blog posts, you have already installed IRB because it comes as default with all the previous installations. 

What's REPL?

But before we can understand IRB, we have to know about REPL. REPL is an interactive top-level 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 terms 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, considering 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 evaluation result and prints it out to the user. It may be pretty printed to make it easier to understand if it is a complex expression.

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 needs to enter some expressions inside the REPL
  • * REPL only lives in memory, so once you shut down the REPL, the data disappear
  • * REPL is an evaluator of the expressions given by the developer and shows a result of an error, so for that reason, some developers call it: an interpreter
  • * It is possible to build a REPL for almost any programming language (even you can create one if you want), and so for pretty much every popular language, there is one that exists (but you don't have to write one by yourself because of this). However, not all languages are distributed originally with a REPL (almost all modern programming languages have a REPL, so don't worry about it)
  • * 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 Replit is an online IDE (integrated development environment). The advantage of cloud environments is that you don't need to install anything locally, but you've to create an account in most cases. 

This kind of cloud environment supports almost any modern programming language, 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:, among others.

1- Accessing IRB from

Create your own account and once you're inside it, click on the blue Create button at the left sidebar. It will prompt you to a modal window where you can choose Ruby as a 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 in the left panel, you just need to click that green button.

After that, you'll see the output in the right panel of the

2- Accessing to IRB from the AWS Linux instance:

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

Now let's switch to our AWS Linux instance. In your console, login into the virtual machine and then access 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 > 

* The first command allows you to enter the virtual machine via ssh keys
* The second command prints your current Ruby version
* The third command, "irb," enter you to the Interactive Ruby Console

Now, you're inside the IRB.

3- Accessing to IRB from 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

* The first command prints your current Ruby version
* The second command "irb" enter you to the Interactive Ruby Console

Now, you're inside the IRB.

Testing Ruby inside IRB

For all the 3 ways we used before to connect to IRB, any of the following commands will work. In my case, I'll focus on the AWS Linux Instance.

1- First steps in IRB

In the first steps (as with any new programming language you're learning), you 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 screenshot with these commands

If you don't understand much of these commands, don't panic, we'll see all of these commands in detail 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 it prints that variable

As I mentioned, we will see all of these terms in the following posts.

Now the question is, what does this first part of the console output?

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 following 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.

The first line of code

What does it mean?

The second line of code

What does it mean?

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

I hope you've learned a lot from this post. I'll see you at the next one.

Thanks for your reading
Daniel Morales