Dialed-in Rails script/console with pretty printing and history

Edit (as root) your /etc/irbrc:

# Some default enhancements/settings for IRB, based on
# http://wiki.rubygarden.org/Ruby/page/show/Irb/TipsAndTricks
 
unless defined? ETC_IRBRC_LOADED
 
  # Require RubyGems by default.
  require 'rubygems'
 
  begin
    require "ap"
    IRB::Irb.class_eval do
      def output_value
        ap @context.last_value
      end
    end
  rescue LoadError => e
    puts "ap gem not found.  Try typing 'gem install awesome_print' to get super-fancy output."
  end
 
  # Activate auto-completion.
  require 'irb/completion'
 
  # Use the simple prompt if possible.
  IRB.conf[:PROMPT_MODE] = :SIMPLE if IRB.conf[:PROMPT_MODE] == :DEFAULT
 
  # Setup permanent history.
  HISTFILE = "~/.irb_history"
  MAXHISTSIZE = 100
  begin
    histfile = File::expand_path(HISTFILE)
    if File::exists?(histfile)
      lines = IO::readlines(histfile).collect { |line| line.chomp }
      puts "Read #{lines.nitems} saved history commands from '#{histfile}'." if $VERBOSE
      Readline::HISTORY.push(* lines)
    else
      puts "History file '#{histfile}' was empty or non-existant." if $VERBOSE
    end
    Kernel::at_exit do
      lines = Readline::HISTORY.to_a.reverse.uniq.reverse
      lines = lines[-MAXHISTSIZE, MAXHISTSIZE] if lines.nitems > MAXHISTSIZE
      puts "Saving #{lines.length} history lines to '#{histfile}'." if $VERBOSE
      File::open(histfile, File::WRONLY|File::CREAT|File::TRUNC) { |io| io.puts lines.join("\n") }
    end
  rescue => e
    puts "Error when configuring permanent history: #{e}" if $VERBOSE
  end
 
  ETC_IRBRC_LOADED=true
end

Thanks to Nick Sieger and Jared Haworth for sharing.

Related posts:

  1. Quick MacPorts Installation of Ruby on Rails
    October 2010 update: I wrote this a while back, before I knew about RVM. The Ruby Version Manager has a bunch of features that makes it better than macports for......
  2. Switching between Rails 2 and Rails 3 on Mac OS X or Ubuntu with RVM
    I’m migrating AdGrok to Rails 3 this weekend–what with the new ActiveRecord query functionality, and MongoMapper, and all our gems migrating to Rails 3, it seems like it’s time. The......
  3. Simple PHP image rotation script
    This blog has a rotating header — if you visit /header/ and bounce on reload, you’ll see a series of random images. Here’s how it works: The header is added......
  4. HOWTO: Fix Ruby on Rails 2.3.x textarea Value Truncation
    Turns out that Rails versions 2.3.6 through 2.3.8 have a pretty horrid parameter-parsing bug, and I’m surprised there hasn’t been more hoopla about it. If you have a textarea form......

  • http://blog.newforge-tech.com garyfleshman

    The title begins, “Dailed-in”. Is this a typo, or a new Rails adjective?

  • http://matthew.mceachen.us matthew

    I thought it was pretty widespread, but it’s a biker colloquialism for getting something sorted out and comfortable. It’s not Rails-specific.

  • http://www.levityisland.com/ Lawrence Wang

    I think Gary was pointing out that it’s “Dialed-in” :)

  • Anonymous

    Oh, burrrrn! Derf!