A better Ruby prompt

The Interactive Ruby Shell, or irb, is indispensable for trying out Ruby code rapidly, seeing what works and what’s elegant. But the defaults aren’t quite optimal.

Tab completion

It lacks tab completion by default. Having this available can save you a lot of time, especially as you are learning the language. To turn it on, simply add the option “-r irb/completion” to require that functionality. You can also add it to a running session by entering “require ‘irb/completion’”. After setting this you can press Tab after a dot following an object to see what methods are available for it (not all methods shown in image):

tab-complete

Now you can type the method you want to use from the list. You can also type part of a method name and press Tab to view matches on what you have so far.

Appearance

The default prompt can also be a little too wordy:

default-irb

This shows the program name (always “irb(main)” for the irb command), the line number within said program, and the indentation level. This last value is incremented as you move into loops and other multi-line structures. If you don’t want this meta information, however, just call irb with the “–simple prompt” option:

simple-irb

Aliases

I combine the above customizations into two Bash aliases for simpler access. Just add the following to your ~/.bashrc:

1
2
alias r="irb --simple-prompt -r irb/completion"
alias irb="irb -r irb/completion"

Now you can get to a simpler prompt for quick checks, or the full prompt for more complete documentation, making a tutorial, etc. And both have the lovely tab completion.

[EDIT, 2009-07-17]: As helpfully pointed out by commenter Vorian, the improvements above can also be attained using configuration options placed into ~/.irbrc. After hearing about this, I looked around for example .irbrc files and put together a decent one. Check out this post for more details.

Share and Enjoy:
  • email
  • LinkedIn
  • Slashdot
  • StumbleUpon
  • Technorati
  • Netvibes

Post to Twitter Post to Delicious Post to Digg Post to Reddit

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

This entry was posted in Ruby and tagged , , , . Bookmark the permalink.

3 Responses to A better Ruby prompt

  1. vorian says:

    why not just use an irbrc?

    # Change the prompt
    IRB.conf[:PROMPT_MODE] = :SIMPLE

  2. @vorian Simply because I didn’t know it existed:-) Thanks for the tip!

  3. Pingback: Improved irb configuration | tail -f findings.out

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>