Skip to content

Instantly share code, notes, and snippets.

@tjh
Forked from unixmonkey/.irbrc
Created December 22, 2009 14:10
Show Gist options
  • Save tjh/261758 to your computer and use it in GitHub Desktop.
Save tjh/261758 to your computer and use it in GitHub Desktop.
# *****************************************
# .irbrc
#
# Tim Harvey - Literacy5
# http://literacy5.com/
#
# Includes tons of visual sugar, based on the
# work of:
#
# UnixMonkey - http://gist.github.com/254551
# Dr. Nic - http://drnicwilliams.com/2006/10/12/my-irbrc-for-consoleirb/
#
# *****************************************
# -----------------------------------------
# Plumbing
# -----------------------------------------
require 'rubygems'
# -----------------------------------------
# Method to clean up hash output
# -----------------------------------------
require 'pp'
# -----------------------------------------
# Tab completion
# -----------------------------------------
require 'irb/completion'
# -----------------------------------------
# Wirble - Coloring and other irb niceties
# -----------------------------------------
begin
require 'wirble'
Wirble.init
Wirble.colorize
rescue LoadError => err
warn "Couldn't load Wirble: #{err}"
end
# -----------------------------------------
# What methods (and result) are available
# -----------------------------------------
begin
require 'what_methods'
rescue LoadError => err
warn "Couldn't load what_methods: #{err}"
end
# -----------------------------------------
# Indent blocks
# -----------------------------------------
IRB.conf[:AUTO_INDENT]=true
# -----------------------------------------
# irb history available between sessions
# -----------------------------------------
HISTFILE = "~/.irb_history" unless Module.constants.member? "HISTFILE"
MAXHISTSIZE = 100 unless Module.constants.member? "MAXHISTSIZE"
begin
if defined? Readline::HISTORY
histfile = File::expand_path( HISTFILE )
if File::exists?( histfile )
lines = IO::readlines( histfile ).collect {|line| line.chomp}
puts "Read %d saved history commands from %s." % [ lines.nitems, histfile ] if $DEBUG || $VERBOSE
Readline::HISTORY.push( *lines )
else
puts "History file '%s' was empty or non-existant." % histfile if $DEBUG || $VERBOSE
end
Kernel::at_exit {
lines = Readline::HISTORY.to_a.reverse.uniq.reverse
lines = lines[ -MAXHISTSIZE, MAXHISTSIZE ] if lines.nitems > MAXHISTSIZE
$stderr.puts "Saving %d history lines to %s." % [ lines.length, histfile ] if $VERBOSE || $DEBUG
File::open( histfile, File::WRONLY|File::CREAT|File::TRUNC ) { |ofh|
lines.each { |line| ofh.puts line }
}
}
end
end
# -----------------------------------------
# Prompt to include project name and env (Rails)
# -----------------------------------------
if rails_env = ENV['RAILS_ENV']
rails_root = File.basename(Dir.pwd)
prompt = "#{rails_root}[#{rails_env}]"
IRB.conf[:PROMPT] ||= {}
IRB.conf[:PROMPT][:RAILS] = {
:PROMPT_I => "#{prompt}> ",
:PROMPT_S => "#{prompt}* ",
:PROMPT_C => "#{prompt}? ",
:RETURN => "=> %s\n"
}
IRB.conf[:PROMPT_MODE] = :RAILS
# Called after the irb session is initialized and Rails has
# been loaded (props: Mike Clark).
IRB.conf[:IRB_RC] = Proc.new do
# Shows what's logged in realtime (beats tailing the log in another terminal)
# ActiveRecord::Base.logger = Logger.new(STDOUT)
# Alias User[3] for User.find(3)
# ActiveRecord::Base.instance_eval { alias :[] :find }
end
end
# -----------------------------------------
# Local methods for an object
# -----------------------------------------
class Object
# Return a list of methods defined locally for a particular object. Useful
# for seeing what it does whilst losing all the guff that's implemented
# by its parents (eg Object).
def local_methods(obj = self)
(obj.methods - obj.class.superclass.instance_methods).sort
end
end
# -----------------------------------------
# Benchmark block run time
# -----------------------------------------
def benchmark
# From http://blog.evanweaver.com/articles/2006/12/13/benchmark/
# Call benchmark { } with any block and you get the wallclock runtime
# as well as a percent change + or - from the last run
cur = Time.now
result = yield
print "#{cur = Time.now - cur} seconds"
puts " (#{(cur / $last_benchmark * 100).to_i - 100}% change)" rescue puts ""
$last_benchmark = cur
result
end
# -----------------------------------------
# Hirb - Pretty ActiveRecord tables, seems
# to require loading at the end to
# avoid conflicts with other tools
# -----------------------------------------
begin
require 'hirb'
Hirb::View.enable
rescue LoadError => err
warn "Couldn't load Hirb: #{err}"
end
# -----------------------------------------
# When in Rails, output the log entries
# that give us nice SQL statements when
# using ActiveRecord
#
# Thanks thoughtbot!
# http://robots.thoughtbot.com/post/159806033/irb-script-console-tips
# -----------------------------------------
if ENV.include?('RAILS_ENV') && !Object.const_defined?('RAILS_DEFAULT_LOGGER')
require 'logger'
RAILS_DEFAULT_LOGGER = Logger.new(STDOUT)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment