Supercharge your irb

October 24th, 2008, by Matthew

I’ve been spending some time in the Rails console lately, and remembered hearing the Rails Envy guys talk about getting more out of irb on a recent podcast. I tracked down the episode and followed the link to Extending Your irb.

Zach gives you a bunch of great tips and provided a .irbrc file, which I used as the base for mine:

#!/usr/bin/ruby
IRB.conf[:AUTO_INDENT]  = true
IRB.conf[:USE_READLINE] = true
IRB.conf[:SAVE_HISTORY] = 1000
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb_history"
IRB.conf[:PROMPT_MODE]  = :SIMPLE

require 'irb/completion'
require 'irb/ext/save-history'
require 'pp'

# add ~/.ruby to the library search path
$LOAD_PATH << File.expand_path('~/.ruby')

# load rubygems, wirble and utility belt
require 'rubygems' rescue nil
require 'wirble'
require 'utility_belt'

# load wirble
Wirble.init
Wirble.colorize

class Object
  # print documentation
  #
  # ri 'Array#pop'
  # Array.ri
  # Array.ri :pop
  # arr.ri :pop
  def ri(method = nil)
    unless method && method =~ /^[A-Z]/ # if class isn't specified
      klass = self.kind_of?(Class) ? name : self.class.name
      method = [klass, method].compact.join('#')
    end
    puts `ri '#{method}'`
  end

  def own_methods
    (self.methods - Class.methods).sort
  end
end

I really like the own_methods method.

I also find it useful to string together some methods to display the object in a more readable format, like puts object.to_yaml or puts object.methods.sort.

We are a Perth web design and web development company and this is our blog. We specialize in building web applications with the Ruby on Rails framework. Jump to the Ruby on Rails category or contact us.

One Response to “Supercharge your irb”

  1. Zach Inglis says:

    Thanks for reposting this. I am glad you found it helpful.

    I am always on the lookout for more optimizations for IRB, so if you have any more suggestions I would love to hear them.

Leave a Reply


Follow Us

Stay in the Loop

  • Enter your email address to subscribe to our mailing list. You'll get updates about our products, specials and bonus offers, and general behind the scenes news from our team.

Twitter

Facebook Fans

Newsletters

Alexa Rank

Testimonial

The boys at The Frontier Group are amazing! For such a relaxed and personable organisation, they have phenomenal technical ability and a rampant professionalism. They have customisable solutions for all of my IT needs and they always deliver, on time and beyond expectation.

They fix problems other service providers can't and they helped me get a critical section of my web site up and running 10 minutes after I emailed the request!

Alex Hyndman, Nexus Car Share.

Featured Project

Case Study - Caudo Group - www.caudo.com.au

Website

www.caudo.com.au

Caudo Machinery

Caudo Group engaged our services to redesign their outdated website. We sent our photographer on-site to capture the essence of their business and turned it into a stunning web design.