Extending your irb

As a Ruby developer, you probably spend a reasonable amount of time in script/console or irb. What you may not have known was that you can extend and give your irb more functionality and make your life easier. I’d like to introduce you to ~/.irbrc.

Let’s add tab method completion as well, that’ll make life more simple.

    require 'irb/completion'
  

One thing that had always annoyed me about irb is the lack of colour differentiating objects, strings, classes, etc. The Wirble gem solves this. I also installed utility_belt which has a bunch of extra features for your irb. (Don’t forget to install it via: sudo gem install wirble utility_belt)

    # load rubygems, wirble and utility_belt
    require 'rubygems'
    require 'wirble'
    require 'utility_belt'    

    # load wirble
    Wirble.init
    Wirble.colorize
  

utility_belt allows us to do a lot of great things, such as editing irb in Textmate, pastie-ing (OS X only), grep methods and classes, Google the last output (good for errors), only having to type x for quit

When debugging, I use ‘pp‘ (aka pretty_print) for examining my objects in Ruby. It’s annoying that I have to require it every time. Instead of having to constantly type in the require command, I have just added it to my irbrc file.

    require 'pp'
  

You can also add methods to the file for later use . I have extended the Object class, to simulate the following: Post.methods - Class.methods

    class Object
      def non_class_methods
        self.methods - Class.methods
      end
    end
  

I also added q as my exit command so i can exit by typing q and hitting return. utility_belt provides x for me, but I am more familiar with q.

alias q exit

You can see this file and all my other dot-files on Github.

10 Responses to “Extending your irb”


  1. 1 Stephan

    Thanks for sharing. One comment though:
    On one hand, when wirble is required (and initialized) from within irb the irb prompt doesn't change.
    If I - on the other hand - require (and init) in .irbrc there's a simple prompt ('>> '). However using the following to init Wirble keeps the original prompt:

    Wirble.init :skip_prompt => true

  2. 2 Bradly Feeley

    Nice tips! I didn't know about the utility_belt gem.

    I also have the following in my .irbrc
    class Object
    alias :original_methods :methods
    def methods; original_methods.sort; end;
    end

  3. 3 Soleone

    Really useful, thanks!!

    I also added this method to Object to show only instance methods of the current instance:

    <pre>
    class Object
    def own_methods
    (self.methods - Object.methods).sort
    end
    end
    </pre>

  4. 4 Soleone

    Unfortunately the builder gem (required by wirble) seems to cause problems with textmate when running tests, can anybody confirm that? I had to uninstall this to make my tests work again from textmate :(

  5. 5 zachinglis

    I have had many issues with testing from inside Textmate. For me, whether the test functionality is going to work is hit or miss and was not brought on by anything else.

  6. 6 Josh Nichols

    My day has been made from this alone: require ‘irb/completion’

    Thanks!

  7. 7 Andreas

    As far as I know, you don’t need to require irb/completion if you use wirble anyway. Wirble adds colored output, command history and tab completion to irb.

    When working on a rails application, I also like to redirect the logging output to the irb console, so I can see the SQL queries when trying out model methods, like described on

    http://zargony.com/2008/04/28/five-tips-for-developing-rails-applications

  8. 8 Career Builder Website

    Have you tried to write a professional sales letter? How about converting visitors to buyers. This program automates the task and creates sales letters, landing pages and website front pages that convert visitors to sales. You must see this program! sales-letter-creator-pro dot com

  1. 1 chinese searches
  2. 2 liger

Leave a Reply