Customizing IRB

March 4, 2023

IRB has changed a bit in recent Ruby versions. It's more colorful, but also clunkier. Here are a few of the recent changes:

Ruby Update .irbrc Option
2.7 Colorization introduced :USE_COLORIZE
2.7 Echo-on-assignment truncates values :OMIT_ON_ASSIGNMENT
2.7 Multiline editing added :USE_MULTILINE
3.0 Echo-on-assignment default changed to :truncate (replacing :OMIT_ON_ASSIGNMENT) :ECHO_ON_ASSIGNMENT
3.0 Default inspector changed from :p to :pp and was colorized :INSPECT_MODE
3.1 Autocompletion displays in a blue box after any keystroke :USE_AUTOCOMPLETE

Colorization (Ruby 2.7)

Colorization has been a nice touch. It can help you quickly spot syntax problems as you type. In Ruby 2.6 if you define a hash in IRB it would look like this:

In Ruby 2.7, it would look like this:

Echo-On-Assignment Truncation (Ruby 2.7)

Starting in Ruby 2.7, the inspection output for assigned values is truncated. This was first managed with the :OMIT_ON_ASSIGNMENT setting and then in Rails 3.0 this was changed to a new :truncate option for the :ECHO_ON_ASSIGNMENT setting.

Truncation might be useful in some circumstances. But in Rails, this is unhelpful. It's common to want to assign an ActiveRecord instance to a variable and then immediately look at all of its attributes. You might want to quickly review values to make sure you loaded the correct record. Or you might want to copy attribute names to the clipboard. In Ruby 2.6 you could do this easily. All attributes would be listed and long data values would be truncated:

But in Ruby 2.7, the entire output is truncated so you have to type the variable name again if you want to see its full contents:

Multiline Editing (Ruby 2.7)

In Ruby 2.7, you can edit multiple lines of input prior to execution. The nice thing about this is that you can go back and fix things that you had mistyped on a previous line. For example, if you had started to define a method called method_name_with_typo:

You can use the arrow keys to backup to an earlier line and change it:

In previous versions of Ruby, you would have been stuck on the most recent line and would have needed to cancel and reenter everything to make a correction to an earlier line.

The downside to multiline editing in Ruby 2.7 is that it is incredibly slow when you want to paste in several lines of Ruby code. The performance was improved in Ruby 3.0.

Default Inspector (Ruby 3.0)

In Ruby 3.0, the default inspector changed from :p to :pp. Assigning an ActiveRecord object to a variable in Ruby 3.0 would now provide almost no information up front. Again you would need to retype the variable to see the full contents:

Autocomplete (Ruby 3.1)

Likewise, the recent autocompletion change in IRB has been jarring. In previous versions of Ruby you could choose to type the tab key twice and get a list of matching keywords which would display above the text you were entering:

In Ruby 3.1, this changed to a blue box that is always in your face as you type any key:

Customization

To change the behavior of IRB, you can make modifications to the IRB.conf in ~/.irbrc. You can see the available configuration options in the IRB. init_config method.

If you're on Ruby 3, the following settings will restore the behavior to what it was like back in Ruby 2.6:

# ~/.irbrc
IRB.conf[:USE_COLORIZE] = false
IRB.conf[:ECHO_ON_ASSIGNMENT] = true
IRB.conf[:USE_MULTILINE] = false
IRB.conf[:INSPECT_MODE] = :p
IRB.conf[:USE_AUTOCOMPLETE] = false

Alternatively, you can pass options when launching irb. This approach makes it easier to set up a global alias for irb that multiple users can share.

irb --nocolorize --echo-on-assignment --nomultiline --inspect p --noautocomplete

You can also pass these options through the Rails console command and onto IRB with:

rails c -- --nocolorize --echo-on-assignment --nomultiline --inspect p --noautocomplete


Revisions
  • March 30, 2023 - Added info about :USE_MULTILINE