Improving IRB experience with a custom .irbrc

Ruby’s IRB is a lovely interactive console. By leveraging a custom .irbrc configuration file, we can make the experience even better.

The .irbrc file is nothing else than a Ruby file that gets evaluated whenever we start the console with irb or rails c. We can place it in a home directory (~/.irbrc) or in the project directory (to scope it per project). But only one of these files will take effect, and the global one has precedence.

So what kind of things can we put there?

We can require gems we already want to have ready:

require 'irb'
require 'irb/completion'
require 'rubygems'
require 'pp'

We can ask IRB to save history:

IRB.conf[:SAVE_HISTORY] = 200
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb-save-history"

We can change the default prompt:

IRB.conf[:PROMPT][:MY_PROMPT] = { # name of prompt mode
  :AUTO_INDENT => true,           # enables 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] = :MY_PROMPT

We can provide IRB’s command line options as settings:

IRB.conf[:IRB_NAME]="irb"
IRB.conf[:MATH_MODE]=false
IRB.conf[:INSPECT_MODE]=nil
IRB.conf[:IRB_RC] = nil
IRB.conf[:BACK_TRACE_LIMIT]=16
IRB.conf[:USE_LOADER] = false
IRB.conf[:USE_READLINE] = nil
IRB.conf[:USE_TRACER] = false
IRB.conf[:IGNORE_SIGINT] = true
IRB.conf[:IGNORE_EOF] = false
IRB.conf[:PROMPT_MODE] = :DEFAULT
IRB.conf[:PROMPT] = {...}
IRB.conf[:DEBUG_LEVEL]=0

We can fix IRB’s lack interop with the ri documentation (in case we don’t have Pry):

def ri(*names)
  system(%{ri #{names.map {|name| name.to_s}.join(" ")}})
end
irb(main):009:0> ri File

We can add some useful debugging methods to Ruby core classes and modules without poluting the application itself:

class Class
  public :include

  # Show only this class class methods
  def class_methods
    (methods - Class.instance_methods - Object.methods).sort
  end

  # Show instance and class methods
  def defined_methods
    methods = {}

    methods[:instance] = new.local_methods
    methods[:class] = class_methods

    methods
  end
end

class Object
  # Show local methods from the object's class
  def defined_methods
    (methods - Object.instance_methods).sort
  end

  # Open a particular method in an editor
  def ocode(method_name)
    file, line = self.method(method_name).source_location

    if file && line
      # or code -g
      `subl '#{file}:#{line}'`
    else
      "'#{method_name}' not found."
    end
  end
end

We can let ActiveRecord log to our IRB session:

if ENV['RAILS_ENV']
  IRB.conf[:IRB_RC] = Proc.new do
    logger = Logger.new(STDOUT)
    ActiveRecord::Base.logger = logger
  end
end

And we can create a lot of helper methods that prepare some data (like a request body) or call something that would be a bit cumbersome to write every time.

← BUY THE PRE-RELEASE

I am writing a complete guide on web application deployment. Ruby with Puma, Python with Gunicorn, NGINX, PostgreSQL, Redis, networking, processes, systemd, backups, and all your usual suspects. 23 chapters and 3 scripted demonstrations already available.

More →