This website uses cookies. If you continue to use this site we will assume that you are happy with this. When you close this box we will save this information in a cookie to ensure you'll never be bothered with this information box. ×

What is REPL?

REPL - an abbreviation of read-eval-print loop - is the interactive tool, specific to interpreted programming languages, which reads the input from the stdin, evaluates it and prints the value to the stdout. You may think the REPL is a kind of shell for Ruby, but there is a difference - Unix Shell gets the command, evaluate it but it is not printing the returned value to the stdout. Instead of this, it is keeping it in $? environment variable.


In Ruby, REPL is called irb - for Interactive RuBy. Inside irb you can launch any ruby command you want, assign variables, define functions, classes, redefine standard Ruby classes - but all the changes will last only with the current irb session. You can treat irb like the Ruby shell: trying out your code, quick testing the functions, learning Ruby. You will spend a lot of time with irb, so get familiar with it!

irb is not the only REPL available for Ruby. Very interesting option is pry, which has many advanced features, like code browsing, great syntax highlighting, editing. For more information, go to the Github:

To run it, launch irb from the shell. To exit, use Ctrl + D or type exit. Below is the example of the REPL session in Ruby:

$ irb
irb(main):001:0> puts 'Hello, world.'
Hello, world.
=> nil
irb(main):002:0> 6 * 7
=> 42
irb(main):003:0> x = 6 * 7
=> 42
irb(main):004:0> x
=> 42
irb(main):005:0> exit

Irb prompt

Like in the Shell, there is a prompt irb(main):001:0> displayed before every keyboard input. Prompt can be changed or even removed. Default prompt contains valuable information: command name, the main object, line number in the current session and indent level.

In all examples in this book IRB prompt is disabled (changed to an empty string), and return value will start with #=> . This is a standard in Ruby documentation (see chapter Getting Help) and makes copy/paste easier.

6 * 7
#=> 42
x = 6 * 7
#=> 42
#=> 42

Evalutating Expressions and Returning Values

When you type the command, REPL evaluates it and prints its return value followed by arrow =>. This means that the result of puts 'Hello, world' is nil (nil is a special Ruby object, which means nothing). Similar the result of multiplication 6 * 7 is 42, the return value of assigment a value to the variable x is this value, and the result of evaluation previously assigned variable x is its value. In Ruby, everything returns value.

If puts returns nil, how can we see the "Hello, world" string in irb session? Note the string is coming before the return value - it appears while evalutating the command. When processing puts method, Ruby prints the string to the stdout, but do not return it. Treating puts as a function, it always returns nil, and the displayed string is a side effect.

The function has a side effects, when in addition to returning value it is also modifing a state of something or it is doing an interaction with outside world. Input and output, manipulating the files - this are the side effects.

Customizing Irb

To view the current configuration, type in conf command:

irb(main):012:0> conf
=> conf.ap_name="irb"
conf.prompt_c="%N(%m):%03n:%i* "
conf.prompt_i="%N(%m):%03n:%i> "
conf.prompt_n="%N(%m):%03n:%i> "
conf.prompt_s="%N(%m):%03n:%i%l "
conf.return_format="=> %s\n"

On start irb is looking for following files in the given order: $HOME/.irbrc, .irbrc, irb.rc, _irbrc, $irbrc and, when found, loads initialization from it. There are few customizations which makes life with irb easier, two of them are shown in the below example .irbrc file:

require 'pp'
require 'irb/ext/save-history'
IRB.conf[:SAVE_HISTORY] = 1000
IRB.conf[:HISTORY_FILE] = "~/.irb_history"

We will take a look on require command later, for now you should know that it is used for loading the additional Ruby files or libraries. The first library is 'pp' - from 'pretty print'. It gives you an alternative to puts command, formatting Ruby object like Arrays or Hashes prettier and more human-readable than default. The second one, 'irb/ext/save-history' is an extention to irb which gives you a possibility to save the history to the file, so in the next session you can recall the previous commands with up arrow - in the same way like in bash or zsh. Next lines configure the number of entries in a history file: IRB.conf[:SAVE_HISTORY] and the location of file itself: IRB.conf[:HISTORY_FILE] = "~/.irb_history".