Join my Laravel for REST API's course on Udemy ๐Ÿ‘€

Becoming productive in the Ruby IRB console

February 24, 2020 ย โ€ย 3ย min read

With the release of Ruby 2.7 the IRB shell got somewhat of a make over. It now includes syntax highlighting, multiline editing, tab completion and RDoc integration. And with some tweaks in the .irbrc file we can improve the overall experience even more.

Since Ruby 2.7 the IRB history is by default written to a history file in your home directory. If you're on an older version of Ruby you can enable the same behavior using the following snippet. This saves the last 1000 entries in the IRB shell.

# ~/.irbrc
IRB.conf[:SAVE_HISTORY] = 1000

Ruby versions older than 2.7 also don't enable indentation by default. You can change this in the irbrc as well. However, this doesn't set the indent correctly for closing ends. Which does work out of the box in Ruby 2.7.

# ~/.irbrc
IRB.conf[:AUTO_INDENT] = true

Loading gems

Some gems come in handy to have available in your shell. You can require them interactively during your IRB session. But if you find yourself doing this with the same gems you can require them in the irbrc file as well.

# ~/.irbrc
require 'faraday'

This works nice until you start using different Ruby versions with a tool like rbenv. These different Ruby versions share the same .irbrc but don't necessarily have the same gems available. If this happens, getting some notice would be nice. We can do this by using the following snippet in your .irbrc.

# ~/.irbrc
%w{faraday faker}.each do |lib|
  begin
    require lib
  rescue LoadError => err
    $stderr.puts err
  end
end

This will give the following message when you start the IRB console.

$ irb
cannot load such file -- faker
irb(main):001:0>

Helper functions

Since the irbrc file is interpreted as a Ruby file you are free to define all the functions you want. Say for example you get annoyed by typing exit all the time when exiting IRB. You can define a function called q that saves you three characters of your time.

# ~/.irbrc
def q
  puts '๐Ÿ‘‹ See you later alligator'
  exit
end

To use this new time saving function you can just call it from IRB.

$ irb
irb(main):001:0> q
๐Ÿ‘‹ See you later alligator

Changing the prompt

The IRB console comes with multiple prompt modes built-in, namely :NULL, :DEFAULT, :CLASSIC, :SIMPLE, :INF_RUBY and :XMP. You can try them out and set your preferred one as follows:

# ~/.irbrc
IRB.conf[:PROMPT_MODE] = :SIMPLE

You can easily create your own prompt mode definition.

# ~/.irbrc
IRB.conf[:PROMPT][:CUSTOM_PROMPT] = {
  :AUTO_INDENT => false,          # disables auto-indent mode
  :PROMPT_I =>  ">> ",            # simple prompt
  :PROMPT_S => nil,               # prompt for continuated strings
  :PROMPT_C => nil,               # prompt for continuated statement
  :RETURN => "    ==>%s\n"        # format to return value
}

IRB.conf[:PROMPT_MODE] = :CUSTOM_PROMPT

For more information about changing the prompt you can read the docs here.