I think that irb
is one of the most useful tools in the rails toolbox. It allows me to quickly determine how something works, sample results (for putting into blogs, don’tcha know) and makes me a more effective ruby developer.
One of the nice things about irb
is it’s .irbrc
file where you can place startup commands. I’d like to share mine (and ask for others to share their favorite .irbrc
tools/contents.
As of today, my .irbrc looks like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
require 'rubygems' require 'wirble' require 'map_by_method' require 'what_methods' require 'pp' IRB.conf[:AUTO_INDENT]=true # start wirble (with color) Wirble.init #Wirble.colorize class Object def my_methods (self.methods - Object.methods).sort end end |
Here’s what it does:
1
Require’s rubygems — several of the following lines refer to gems, so this is needed in order to find the data
2
Wirble provides some very useful tools for irb — from colorizing to shortcut methods. One of the shortcut methods allows you to see the methods an object has:
1 2 3 4 5 |
>> po "foo" => ["%", "*", "+", "<<", "[]", "[]=", "all?", "any?", "between?", "capitalize", "capitalize!", "casecmp", "center", "chomp", "chomp!", "chop", "chop!", "collect", "concat", "count", "crypt", "delete", "delete!", "detect", "downcase", "downcase!", "dump", "each", "each_byte", "each_line", "each_with_index", "empty?", "entries", "find", "find_all", "grep", "gsub", "gsub!", "hex", "index", "inject", "insert", "intern", "length", "ljust", "lstrip", "lstrip!", "map", "match", "max", "member?", "min", "next", "next!", "oct", "partition", "reject", "replace", "reverse", "reverse!", "rindex", "rjust", "rstrip", "rstrip!", "scan", "select", "size", "slice", "slice!", "sort", "sort_by", "split", "squeeze", "squeeze!", "strip", "strip!", "sub", "sub!", "succ", "succ!", "sum", "swapcase", "swapcase!", "to_f", "to_i", "to_str", "to_sym", "tr", "tr!", "tr_s", "tr_s!", "unpack", "upcase", "upcase!", "upto", "zip"] >> po("foo") => ["%", "*", "+", "<<", "[]", "[]=", "all?", "any?", "between?", "capitalize", "capitalize!", "casecmp", "center", "chomp", "chomp!", "chop", "chop!", "collect", "concat", "count", "crypt", "delete", "delete!", "detect", "downcase", "downcase!", "dump", "each", "each_byte", "each_line", "each_with_index", "empty?", "entries", "find", "find_all", "grep", "gsub", "gsub!", "hex", "index", "inject", "insert", "intern", "length", "ljust", "lstrip", "lstrip!", "map", "match", "max", "member?", "min", "next", "next!", "oct", "partition", "reject", "replace", "reverse", "reverse!", "rindex", "rjust", "rstrip", "rstrip!", "scan", "select", "size", "slice", "slice!", "sort", "sort_by", "split", "squeeze", "squeeze!", "strip", "strip!", "sub", "sub!", "succ", "succ!", "sum", "swapcase", "swapcase!", "to_f", "to_i", "to_str", "to_sym", "tr", "tr!", "tr_s", "tr_s!", "unpack", "upcase", "upcase!", "upto", "zip"] >> |
More information can be found at http://pablotron.org/software/wirble/.
3
map_by_method allows me to say
1 2 3 4 5 6 7 8 9 10 11 12 13 |
>> class Person >> attr_accessor :name >> ?> def initialize(name) >> @name = name >> end >> end => nil >> people = %w(Tom Dick Mary John).inject([]){|s,name| s.push(Person.new(name))} => [#<Person:0xb7975fd8 @name="Tom">, #<Person:0xb7975f9c @name="Dick">, #<Person:0xb7975f60 @name="Mary">, #<Person:0xb7975f24 @name="John">] >> people.map_by_name => ["Tom", "Dick", "Mary", "John"] >> |
instead of having to say:
1 2 |
>> people.map {|person| person.name} => ["Tom", "Dick", "Mary", "John"] |
You can also do people.map_by_name_and_age
if the elements of the people array quack respond to age. You can install map_by_method
by typing
1 |
sudo gem install map_by_method |
4
what_methods
allows me to do the following to determine which method(s) supply a particular result:
1 2 3 4 |
>> "foo".what? 3 "foo".length == 3 "foo".size == 3 => ["length", "size"] |
It can be installed with
1 |
sudo gem install what_methods |
5
pp
is part of the default ruby libraries. It allows you to “pretty print” objects.
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 |
>> p ENV {"GPG_AGENT_INFO"=>"/tmp/seahorse-3ASU53/S.gpg-agent:6473:1", "SHELL"=>"/bin/bash", "DESKTOP_STARTUP_ID"=>"", "TERM"=>"xterm", "XDG_SESSION_COOKIE"=>"001260665eeed4ab2ee29db248515d84-1218854000.422511-1381954515", "GTK_RC_FILES"=>"/etc/gtk/gtkrc:/home/matt/.gtkrc-1.2-gnome2", "WINDOWID"=>"41945672", "USER"=>"matt", "LS_COLORS"=>"no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.svgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:", "SSH_AUTH_SOCK"=>"/tmp/keyring-nsSYws/ssh", "GNOME_KEYRING_SOCKET"=>"/tmp/keyring-nsSYws/socket", "SESSION_MANAGER"=>"local/hunin:/tmp/.ICE-unix/6366", "USERNAME"=>"matt", "PATH"=>"/home/matt/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games", "DESKTOP_SESSION"=>"default", "GDM_XSERVER_LOCATION"=>"local", "PWD"=>"/tmp", "LANG"=>"en_US.UTF-8", "GNOME_KEYRING_PID"=>"6365", "GDM_LANG"=>"en_US.UTF-8", "GDMSESSION"=>"default", "HISTCONTROL"=>"ignoreboth", "SHLVL"=>"1", "HOME"=>"/home/matt", "GNOME_DESKTOP_SESSION_ID"=>"Default", "LOGNAME"=>"matt", "XDG_DATA_DIRS"=>"/usr/local/share/:/usr/share/:/usr/share/gdm/", "DBUS_SESSION_BUS_ADDRESS"=>"unix:abstract=/tmp/dbus-dleC1hpZrT,guid=6079c71e9a7b1a92cf0295df48a63c72", "LESSOPEN"=>"| /usr/bin/lesspipe %s", "WINDOWPATH"=>"7", "DISPLAY"=>":0.0", "LESSCLOSE"=>"/usr/bin/lesspipe %s %s", "COLORTERM"=>"gnome-terminal", "XAUTHORITY"=>"/tmp/.gdmUWWUFU", "OLDPWD"=>"/home/matt/.emacs.d", "_"=>"/usr/bin/irb", "LINES"=>"24", "COLUMNS"=>"80"} => nil >> puts ENV.to_s ENV => nil >> p ENV {"GPG_AGENT_INFO"=>"/tmp/seahorse-3ASU53/S.gpg-agent:6473:1", "SHELL"=>"/bin/bash", "DESKTOP_STARTUP_ID"=>"", "TERM"=>"xterm", "XDG_SESSION_COOKIE"=>"001260665eeed4ab2ee29db248515d84-1218854000.422511-1381954515", "GTK_RC_FILES"=>"/etc/gtk/gtkrc:/home/matt/.gtkrc-1.2-gnome2", "WINDOWID"=>"41945672", "USER"=>"matt", "LS_COLORS"=>"no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.svgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:", "SSH_AUTH_SOCK"=>"/tmp/keyring-nsSYws/ssh", "GNOME_KEYRING_SOCKET"=>"/tmp/keyring-nsSYws/socket", "SESSION_MANAGER"=>"local/hunin:/tmp/.ICE-unix/6366", "USERNAME"=>"matt", "PATH"=>"/home/matt/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games", "DESKTOP_SESSION"=>"default", "GDM_XSERVER_LOCATION"=>"local", "PWD"=>"/tmp", "LANG"=>"en_US.UTF-8", "GNOME_KEYRING_PID"=>"6365", "GDM_LANG"=>"en_US.UTF-8", "GDMSESSION"=>"default", "HISTCONTROL"=>"ignoreboth", "SHLVL"=>"1", "HOME"=>"/home/matt", "GNOME_DESKTOP_SESSION_ID"=>"Default", "LOGNAME"=>"matt", "XDG_DATA_DIRS"=>"/usr/local/share/:/usr/share/:/usr/share/gdm/", "DBUS_SESSION_BUS_ADDRESS"=>"unix:abstract=/tmp/dbus-dleC1hpZrT,guid=6079c71e9a7b1a92cf0295df48a63c72", "LESSOPEN"=>"| /usr/bin/lesspipe %s", "WINDOWPATH"=>"7", "DISPLAY"=>":0.0", "LESSCLOSE"=>"/usr/bin/lesspipe %s %s", "COLORTERM"=>"gnome-terminal", "XAUTHORITY"=>"/tmp/.gdmUWWUFU", "OLDPWD"=>"/home/matt/.emacs.d", "_"=>"/usr/bin/irb", "LINES"=>"24", "COLUMNS"=>"80"} => nil >> pp ENV {"GPG_AGENT_INFO"=>"/tmp/seahorse-3ASU53/S.gpg-agent:6473:1", "SHELL"=>"/bin/bash", "DESKTOP_STARTUP_ID"=>"", "TERM"=>"xterm", "XDG_SESSION_COOKIE"=> "001260665eeed4ab2ee29db248515d84-1218854000.422511-1381954515", "GTK_RC_FILES"=>"/etc/gtk/gtkrc:/home/matt/.gtkrc-1.2-gnome2", "WINDOWID"=>"41945672", "USER"=>"matt", "LS_COLORS"=> "no=00:fi=00:di=01;34:ln=01;36:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.svgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:", "SSH_AUTH_SOCK"=>"/tmp/keyring-nsSYws/ssh", "GNOME_KEYRING_SOCKET"=>"/tmp/keyring-nsSYws/socket", "SESSION_MANAGER"=>"local/hunin:/tmp/.ICE-unix/6366", "USERNAME"=>"matt", "PATH"=> "/home/matt/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games", "DESKTOP_SESSION"=>"default", "GDM_XSERVER_LOCATION"=>"local", "PWD"=>"/tmp", "LANG"=>"en_US.UTF-8", "GNOME_KEYRING_PID"=>"6365", "GDM_LANG"=>"en_US.UTF-8", "GDMSESSION"=>"default", "HISTCONTROL"=>"ignoreboth", "SHLVL"=>"1", "HOME"=>"/home/matt", "GNOME_DESKTOP_SESSION_ID"=>"Default", "LOGNAME"=>"matt", "XDG_DATA_DIRS"=>"/usr/local/share/:/usr/share/:/usr/share/gdm/", "DBUS_SESSION_BUS_ADDRESS"=> "unix:abstract=/tmp/dbus-dleC1hpZrT,guid=6079c71e9a7b1a92cf0295df48a63c72", "LESSOPEN"=>"| /usr/bin/lesspipe %s", "WINDOWPATH"=>"7", "DISPLAY"=>":0.0", "LESSCLOSE"=>"/usr/bin/lesspipe %s %s", "COLORTERM"=>"gnome-terminal", "XAUTHORITY"=>"/tmp/.gdmUWWUFU", "OLDPWD"=>"/home/matt/.emacs.d", "_"=>"/usr/bin/irb", "LINES"=>"24", "COLUMNS"=>"80"} => nil >> |
6
This line will automatically indent code as it is typed into irb.
8
This line initializes wirble.
9
This line allows wirble to use color — I’ve turned it off for now; running irb in emacs leaves lots of terminal code artifacts when this is turned on.
12-16
This method is a little something I wrote which shows the methods of an object — those which it does not have in common with Object
. It also sorts them — which was suggested by Joe OBrien of The EdgeCase.
So, what’s in your .irbrc
?
2 comments
Chris
August 17, 2008 at 7:40 am (UTC -5) Link to this comment
You can see my .irbrc at http://code.tie-rack.org/cms/irbrc.
Mine is wrapped in an unless condition to make sure that Rails’ script/server isn’t running (up until Rails 2.0, I think, script/server would load .irbrc at the beginning of every request, which could cause issues). I share this across multiple machines, some of which still use Rails 1.2, so that’s still necessary.
I’ve also (at the end there) got some gems that load only if I’m currently using Ruby 1.8.6 and not JRuby.
And I use a little IRB.conf[:IRB_RC] trick to get one gem to load after everything else (including Rails) so that it doesn’t mess up some of Rails’ magic.
Matt Williams
August 18, 2008 at 6:09 pm (UTC -5) Link to this comment
Thanks for sharing! There’s a lot of neat ideas there. I plan to incorporate some of them.