Improved irb configuration

As applies to most new tools I start to use, the time in which I’ve started to learn Ruby has included a fair amount of time improving my general working environment, to make it more suited to the use of a welcome new tool. Aside from seeking out informative and reliable sources of documentation, new useful modules, and additions to my editor to make it more Ruby-friendly, I’ve also sought a more efficient configuration for irb. I discussed irb and how to setup some basic improved config in this recent post.

Commenter Vorian helpfully mentioned that the same effect can be achieved through an .irbrc file. This file can then be placed in a VCS, and in addition having the configuration in one spot is preferable. While seeking information on the configuration options available, I came across a number of additional customizations that have proved quite useful. I took this page’s suggestions mostly verbatim in formulating mine. This page was also helpful. Here’s my current .irbrc, highlights to follow:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# Interactive Ruby console configuration
IRB_START_TIME = Time.now

# Print to yaml format with "y"
require 'yaml'
# Pretty printing
require 'pp'
# Ability to load rubygem modules
require 'rubygems'
# Tab completion
require 'irb/completion'
# Save irb sessions to history file
require 'irb/ext/save-history'

# Not stdlib
require 'map_by_method'
require 'what_methods'
# For printing time in session
require 'duration'
# For coloration
require 'wirble'

# Include line numbers and indent levels:
IRB.conf[:PROMPT][:SHORT] = {
  :PROMPT_C=>"%03n:%i* ",
  :RETURN=>"%s\n",
  :PROMPT_I=>"%03n:%i> ",
  :PROMPT_N=>"%03n:%i> ",
  :PROMPT_S=>"%03n:%i%l "
}

IRB.conf[:PROMPT_MODE] = :SHORT
# Adds readline functionality
IRB.conf[:USE_READLINE] = true
# Auto indents suites
IRB.conf[:AUTO_INDENT] = true
# Where history is saved
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb-save-history"
# How many lines to save
IRB.conf[:SAVE_HISTORY] = 1000

# Turn turn on colorization, off other wirble wierdness
Wirble.init(:skip_prompt => true, :skip_history => true)
Wirble.colorize

# Quick benchmarking facility
# Based on rue's irbrc => http://pastie.org/179534
def quick(repetitions=100, &block)
  require 'benchmark'
  Benchmark.bmbm do |b|
    b.report {repetitions.times &block}
  end
  nil
end

# Return only the methods not present on basic objects
class Object
  def interesting_methods
    (self.methods - Object.new.methods).sort
  end
end

# Prints how long the session has been open upon exit
at_exit { puts Duration.new(Time.now - IRB_START_TIME) }

You’ll need to install the following gems (“sudo gem install FOO”): map_by_method, what_methods, duration, and wirble. Most options should be self-explanatory and there’s plenty of commentary.

With this in place, you’ll get tab completion to assist in remembering method names and automatic indentation to make visualizing your code’s structure simpler. Output will be colorized for quicker understanding. Your activity will be recorded for later review, and when you exit you’ll find out how long you were in the session.

irb

The two methods defined are helpers for benchmarking and viewing the methods on an object that are more interesting:

methods

For more information on irb use, check out this section in Programming Ruby.

Be Sociable, Share!

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

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

4 Responses to Improved irb configuration

  1. Pingback: A better Ruby prompt | tail -f findings.out

  2. Sobber says:

    Really good post, using parts of that for my own .irbrc now.

  3. Alex Fortuna says:

    Nice hacks.

    Regarding `interesting_methods`, there’s a more systematic approach offered by ORI gem — interactive method list with regular expressions and a bunch of neat filtering options.

    Also, you get a _working_ RI in your IRB console.

    Info: http://github.com/dadooda/ori.

    Watch intro screencast.

    ### Setup (`~/.irbrc`) ###

    1
    2
    require "rubygems"
    require "ori"

    ### Request RI on a Class ###

    1
    2
    3
    4
    5
    Array.ri
    String.ri
    [].ri
    "".ri
    5.ri

    ### Request RI on a Method ###

    1
    2
    3
    4
    String.ri :upcase
    "".ri :upcase
    [].ri :sort
    Hash.ri :[]

    ### Request Interactive Method List ###

    1
    2
    3
    4
    5
    String.ri //
    "".ri //
    "".ri /case/
    "".ri /^to_/
    User.ri /^validates_/

    etc.

    Cheers!

  4. Wow, that looks super handy! Thanks, Alex! I will have to play with that some more and blog about it in specific. I got it installed and was able to get the interactive method searches working (“String.ri //”, etc). However running it on simple class and specific methods didn’t seem to work:

    1
    2
    3
    4
    5
    6
    7
    8
    >> String.ri :upcase
    Looking up topics [String#upcase] .
    No articles found
    => nil
    >> Array.ri
    Looking up topics [Array] .
    No articles found
    => nil

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>