I spend most of my time working with IRB (Interactive Ruby) or Rails console. Over time, I've settled on a configuration that provides essential features like persistent command history, autocompletion, and Rails-specific logging. These improvements make the Ruby REPL much more productive for daily development work.
What is IRB?
IRB (Interactive Ruby) is Ruby's built-in REPL (Read-Eval-Print Loop) – an interactive shell for experimenting with Ruby code. It's invaluable for testing code snippets, debugging, and exploring APIs. When working with Rails, the Rails console is essentially IRB with your application's environment loaded.
The Problem
By default, IRB doesn't remember your command history between sessions. If you close IRB and reopen it, all your previous commands are lost. This makes it frustrating to recall useful commands or repeat experiments from earlier sessions.
The Solution: Custom .irbrc Configuration
You can customize IRB's behavior by creating a .irbrc file in your home directory. This file runs automatically whenever IRB starts, allowing you to set up your preferred environment.
Installation
Create or edit ~/.irbrc and add the following configuration:
#! /usr/bin/env ruby
# -*- Ruby-*-
require 'irb/completion'
require 'irb/ext/save-history'
ARGV.concat [ "--readline",
"--prompt-mode",
"simple" ]
# 100 entries in the list
IRB.conf[:SAVE_HISTORY] = 100
# Store results in home directory with specified file name
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb-history"
script_console_running = ENV.include?('RAILS_ENV') && IRB.conf[:LOAD_MODULES] && IRB.conf[:LOAD_MODULES].include?('console_with_helpers')
rails_running = ENV.include?('RAILS_ENV') && !(IRB.conf[:LOAD_MODULES] && IRB.conf[:LOAD_MODULES].include?('console_with_helpers'))
irb_standalone_running = !script_console_running && !rails_running
if script_console_running
require 'logger'
Object.const_set(:RAILS_DEFAULT_LOGGER, Logger.new(STDOUT))
end
What Each Setting Does
Autocompletion:
require 'irb/completion'
Enables tab completion for method names, constants, and variables. Press Tab to see available options.
History Persistence:
require 'irb/ext/save-history'
IRB.conf[:SAVE_HISTORY] = 100
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb-history"
Saves the last 100 commands to ~/.irb-history. Use the up/down arrow keys to navigate through previous commands, even from past sessions.
Readline Support:
ARGV.concat [ "--readline", "--prompt-mode", "simple" ]
Enables readline for better line editing (Ctrl+A, Ctrl+E, etc.) and sets a cleaner, simpler prompt.
Rails Console Detection:
script_console_running = ENV.include?('RAILS_ENV') && ...
Detects whether you're running IRB standalone or within a Rails console, allowing for environment-specific configurations.
Rails Logging:
if script_console_running
require 'logger'
Object.const_set(:RAILS_DEFAULT_LOGGER, Logger.new(STDOUT))
end
When running Rails console with the old script/console command, this outputs SQL queries and other logs directly to the console, making it easier to see what's happening behind the scenes.
Benefits
Persistent History: Never lose that perfect command you wrote yesterday. Your command history persists across sessions.
Tab Completion: Save time by using Tab to autocomplete method names and explore object APIs.
Better Prompt: A cleaner prompt that doesn't clutter your screen with unnecessary information.
Rails-Aware: Automatically adjusts behavior when you're in a Rails console versus standalone IRB.
Usage
After creating the .irbrc file, simply start IRB or Rails console as usual:
# Standalone IRB
irb
# Rails console
rails console
Now try these features:
- Type a few commands, exit IRB, restart it, and press the up arrow – your history is there!
- Type
"hello".reand press Tab to see methods starting with "re" - In Rails console, run a query and see the SQL output
Notes
You can adjust IRB.conf[:SAVE_HISTORY] to save more or fewer commands. I find 100 to be a good balance between having enough history and keeping the history file manageable.
For Rails 3+ applications, logging to STDOUT is typically handled automatically, so the logger setup is mainly useful for older Rails applications using script/console.
View the original code on Gist.
4 replies on “IRB Console with History and Logging”
Do these settings go in the irb settings file? The post isn’t clear about what to *do* with all that text.
Thanks for pointing out. These goes in ~/.irbrc
On line 17 of the code preview your logic is:
result = !condition_a && !condition_b
From boolean algebra,
a’.b’ = (a + b)’
Reducing it in terms of boolean algebra, a more correct, and understandable statement could be
result = ! (condition_a || condition_b)
The statement would become:
irb_console_running = (rails_running or script_console_running) ‘
This is also more intuitive. 🙂
That is one way. If you like it you can change it in your.irbrc 🙂