What's new in Interactive Ruby Shell (IRB) with Ruby 2.7


Many useful features like syntax highlighting, multiline editing, display documents, enable auto indent, enable save and load history by default are introduced in IRB with Ruby 2.7.

Let’s take a look at these features in detail.

Syntax highlighting

Before Ruby 2.7, code on the IRB was only black and white.

Ruby 2.7 has introduced syntax highlighting in IRB inspired by pry.gem to source lines, REPL input, and inspect the output of some core-class objects.

Keywords, core-class objects are now colorized as shown in the above image.

Introduce multiline mode by Reline

Before Ruby 2.7, you must have experienced an issue with navigating to previous lines when executing multiple lines of code on IRB.

You probably have pressed up arrow key (↑) multiple times to get to the required line as IRB loads one line at a time.

I had to press up arrow key more than 50 times to get each line of method definition of welcome_message.

There are ways like reverse-i-search or editing the content in other editors and then pasting on IRB. But it is not convenient when we are trying out something on IRB.

Ruby 2.7 has enabled multi-line editing which is powered by reline, readline compatible pure Ruby implementation.

With this feature, we can now load the whole method definition with just a single press of up arrow key. We can navigate on any line in the method and edit it just like a text editor.

Show documentation when completion.

RDoc installs all documents to Ruby’s directory by default. It can then be used with ri command to get Ruby documentation.

Before Ruby 2.7, pressing TAB key gives suggestions as follows

Ruby 2.7 has provided a new feature that shows documentation when TAB key is pressed twice.

Documentation will look like,

Enable auto indent

Before Ruby 2.7, auto indentation is turned off by default in irb. It can be turned on by adding following to .irbrc:

irb.conf[:AUTO_INDENT] = true

With Ruby 2.7, indentation will be turned on by default. It can be turned off by changing AUTO_INDENT config to false in .irbrc

irb.conf[:AUTO_INDENT] = false

Save and load history by default

Before Ruby 2.7, IRB disables history by default and does not store any commands that we use.

We can enable history by adding the following to .irbrc

irb.conf[:SAVE_HISTORY] = 1000

This will now save last 1000 commands in ~/.irb_history.

With Ruby 2.7, history will be saved and restored by default.

Its refreshing to see so many improvements in the built-in IRB. More improvements are being made in this area for us to look forward to.