Supercharge your irb
Posted in Code, Websites or Tools
I’ve been spending some time in the Rails console lately, and remembered hearing the Rails Envy guys talk about getting more out of irb on a recent podcast. I tracked down the episode and followed the link to Extending Your irb.
Zach gives you a bunch of great tips and provided a .irbrc file, which I used as the base for mine:
#!/usr/bin/ruby
IRB.conf[:AUTO_INDENT] = true
IRB.conf[:USE_READLINE] = true
IRB.conf[:SAVE_HISTORY] = 1000
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb_history"
IRB.conf[:PROMPT_MODE] = :SIMPLE
require 'irb/completion'
require 'irb/ext/save-history'
require 'pp'
# add ~/.ruby to the library search path
$LOAD_PATH << File.expand_path('~/.ruby')
# load rubygems, wirble and utility belt
require 'rubygems' rescue nil
require 'wirble'
require 'utility_belt'
# load wirble
Wirble.init
Wirble.colorize
class Object
# print documentation
#
# ri 'Array#pop'
# Array.ri
# Array.ri :pop
# arr.ri :pop
def ri(method = nil)
unless method && method =~ /^[A-Z]/ # if class isn't specified
klass = self.kind_of?(Class) ? name : self.class.name
method = [klass, method].compact.join('#')
end
puts `ri '#{method}'`
end
def own_methods
(self.methods - Class.methods).sort
end
end
I really like the own_methods method.
I also find it useful to string together some methods to display the object in a more readable format, like puts object.to_yaml or puts object.methods.sort.
Zach Inglis
Nov 3, 2008
Thanks for reposting this. I am glad you found it helpful.
I am always on the lookout for more optimizations for IRB, so if you have any more suggestions I would love to hear them.