irb tab completion

May 7th, 2007 at 5:49 pm

irb is the interactive Ruby shell – an incredibly useful tool during development. Lisp folks are proud of the REPL (Read-Evaluate-Print Loop) – well, irb is REPL for Ruby.

What turns irb into an even more useful instrument, however, is tab completion. First, it makes coding quicker by completing known symbols (kind of an Intellisense, if you’d like). Second, and more importantly, it allows you to quickly see which methods are implemented for objects, by typing:

irb(main):001:0> some_object.

And pressing TAB twice, irb will list all the methods.

For some reason, however, irb doesn’t come with tab completion out of the box. To set it up, you can create a batch file called irbb.bat, for instance, which calls:

irb -r irb/completion

And call that instead of irb. A simpler method, IMHO, is directly edit the irb.bat file in the Ruby installation bin/ directory to require irb/completion.

P.S: I describe Windows solutions. It should be very similar for Linux.

Related posts:

  1. Book review: “The Ruby way” by Hal Fulton
  2. Book review: “Best of Ruby Quiz” by James Gray
  3. ruby
  4. Setting up an initialization file for CLISP on Windows
  5. Book review: “Programming Ruby, 2nd Ed.” by Dave Thomas and Andy Hunt

9 Responses to “irb tab completion”

  1. Martijn EnglerNo Gravatar Says:

    Thanks for the tip!
    To do this on my Mac I used:
    $ vi .profile
    add the following line to the end (shift+G, i for the non-vim-ers ;-) ):
    alias irb=’irb -r irb/completion’
    save & exit (:x)
    $ . .profile
    $ irb

    We’ve got working tab completion! Didn’t test this on my Linux/FreeBSD-box yet, but it should be about the same.

  2. SteveNo Gravatar Says:

    This and a few other IRB enhancements are bundled up in a Gem called wirble: http://pablotron.org/software/wirble/

  3. Richard HeycockNo Gravatar Says:

    Good call. I’ve always found the list of methods on an object by using .methods. But no more! And this does more than just print the methods. Life’s Good.

    Martin that does work on Linux. I tend to use bash functions but both work.

  4. Sergei MatusevychNo Gravatar Says:

    Hey, why not just add the line require 'irb/completion' to your ~/.irbrc file?

  5. elibenNo Gravatar Says:

    Where is the ~/.irbrc file on Windows ? (this is the system this post talks about)

  6. Sergei MatusevichNo Gravatar Says:

    Where is the ~/.irbrc file on Windows?

    I guess irb just uses HOME environment variable to locate the .irbrc file, so you have to set up this variable if you don’t have it already, start the new cmd session and try it out.

  7. Bill NaceNo Gravatar Says:

    On OS X, with the default ruby install, this doesn’t work. There is no “irb/completion” file for the require to find. Anyone got a work-around?

  8. Johan TamNo Gravatar Says:

    Thanks for your solution.

  9. RonaldNo Gravatar Says:

    There’s quite a few more good editing tips for irb to be found at http://www.tortoiseandachilles.com/2007/09/improve-your-interactive-programming.html It’s amazing how much more useful irb is once you turn it into a decent editing environment. I use it constantly now, and Rails’ console as well.