My .irbrc

| Comments

Some collected recipes to make irb and script/console a bit nicer to use:

  • pretty printing (pp whatever)
  • enhanced tab completion
  • rails logging to console
  • saves command history between sessions
  • use readline extensions module

Tips were collected from posts by “Dr. Nic”:http://drnicwilliams.com/2006/10/12/my-irbrc-for-consoleirb/, “Toolman Tim”:http://toolmantim.com/article/2007/2/6/system_wide_script_console_logging and “Dzone Snippets”:http://snippets.dzone.com/posts/show/4371.

~/.irbrc

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  require 'pp'
  require 'irb/completion'
  ARGV.concat [ "--readline", "--prompt-mode", "simple" ]
  IRB.conf[:AUTO_INDENT]=true

  # load console_with_helpers if possible
  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

  # log Rails stuff to STDOUT
  if script_console_running
    require 'logger'
    Object.const_set(:RAILS_DEFAULT_LOGGER, Logger.new(STDOUT))
  end

  # Keep command-line history between startups
  require 'irb/ext/save-history'
  IRB.conf[:SAVE_HISTORY] = 100
  IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb-save-history"

Update: my friend “Joannou”:http://joannou.tumblr.com/ pointed out Utility Belt which looks pretty nice also.

Update 2 (a month later): Been using Utility Belt for a while and noticed some problems… it has a tendency to conflict with ActiveRecord validations and trigger bogus errors during callbacks. Also seems to destroy some of the init process with certain plugins like ActiveScaffold. Perhaps it’s too clever. I ended up rolling back to my old .irbc - YMMV.

Comments