Create a gist now

Instantly share code, notes, and snippets.

This is my current irbrc file. Lots of helpful things I've collected over the years. Enjoy
script_console_running = ENV.include?('RAILS_ENV') && IRB.conf[:LOAD_MODULES] && IRB.conf[:LOAD_MODULES].include?('console_with_helpers')
if script_console_running # log SQL for Rails 2
require 'logger'
Object.const_set(:RAILS_DEFAULT_LOGGER, Logger.new(STDOUT))
end
# log SQL for Rails 3
ActiveRecord::Base.logger = Logger.new(STDOUT) if defined? Rails::Console
require 'bigdecimal'
if defined?(BigDecimal)
BigDecimal.class_eval do
def inspect
to_s
end
end
end
# autoindent of code while typing it
IRB.conf[:AUTO_INDENT]=true
ANSI = {}
ANSI[:RESET] = "\033[0m"
ANSI[:BOLD] = "\033[1m"
ANSI[:UNDERLINE] = "\033[4m"
ANSI[:LGRAY] = "\033[0;37m"
ANSI[:GRAY] = "\033[1;30m"
ANSI[:BLACK] = "\033[0;30m"
ANSI[:RED] = "\033[31m"
ANSI[:GREEN] = "\033[32m"
ANSI[:YELLOW] = "\033[33m"
ANSI[:BLUE] = "\033[34m"
ANSI[:MAGENTA] = "\033[35m"
ANSI[:CYAN] = "\033[36m"
ANSI[:WHITE] = "\033[37m"
# Build a simple colorful IRB prompt
if IRB.conf[:PROMPT]
IRB.conf[:PROMPT][:SIMPLE_COLOR] = {
:PROMPT_I => ">> ",
:PROMPT_N => ">> ",
:PROMPT_C => "?> ",
:PROMPT_S => "?'> ",
:RETURN => "=> %s\n",
:AUTO_INDENT => true }
IRB.conf[:PROMPT_MODE] = :SIMPLE_COLOR
end
# Some default enhancements/settings for IRB, based on
# http://wiki.rubygarden.org/Ruby/page/show/Irb/TipsAndTricks
unless defined? ETC_IRBRC_LOADED
# Setup permanent history.
HISTFILE = "~/.irb_history"
MAXHISTSIZE = 100
begin
histfile = File::expand_path(HISTFILE)
if File::exists?(histfile)
lines = IO::readlines(histfile).collect { |line| line.chomp }
puts "Read #{lines.nitems} saved history commands from '#{histfile}'." if $VERBOSE
Readline::HISTORY.push(*lines)
else
puts "History file '#{histfile}' was empty or non-existant." if $VERBOSE
end
Kernel::at_exit do
lines = Readline::HISTORY.to_a.reverse.uniq.reverse
lines = lines[-MAXHISTSIZE, MAXHISTSIZE] if lines.length > MAXHISTSIZE
puts "Saving #{lines.length} history lines to '#{histfile}'." if $VERBOSE
File::open(histfile, File::WRONLY|File::CREAT|File::TRUNC) { |io| io.puts lines.join("\n") }
end
rescue => e
puts "Error when configuring permanent history: #{e}" if $VERBOSE
end
ETC_IRBRC_LOADED=true
end
class Object
# Easily print methods local to an object's class
def lm
(methods - Object.instance_methods).sort
end
# look up source location of a method
def sl(method_name)
self.method(method_name).source_location rescue "#{method_name} not found"
end
def mate(method_name)
file, line = self.sl(method_name)
if file && line
`mate '#{file}' -l #{line}`
else
"'#{method_name}' not found :( Try #{self.name}.lm to see available methods"
end
end
end
# copy a string to the clipboard
def cp(string)
`echo "#{string}" | pbcopy`
string
end
ANSI_BOLD = "\033[1m"
ANSI_RESET = "\033[0m"
ANSI_LGRAY = "\033[0;37m"
ANSI_GRAY = "\033[1;30m"
def pm(obj, *options) # Print methods
methods = obj.methods
methods -= Object.methods unless options.include? :more
filter = options.select {|opt| opt.kind_of? Regexp}.first
methods = methods.select {|name| name =~ filter} if filter
data = methods.sort.collect do |name|
method = obj.method(name)
if method.arity == 0
args = "()"
elsif method.arity > 0
n = method.arity
args = "(#{(1..n).collect {|i| "a#{i}"}.join(", ")})"
elsif method.arity < 0
n = -method.arity
args = "(#{(1..n).collect {|i| "a#{i}"}.join(", ")}, ...)"
end
klass = $1 if method.inspect =~ /Method: (.*?)#/
[name, args, klass]
end
max_name = data.collect {|item| item[0].size}.max
max_args = data.collect {|item| item[1].size}.max
data.each do |item|
print " #{ANSI_BOLD}#{item[0].rjust(max_name)}#{ANSI_RESET}"
print "#{ANSI_GRAY}#{item[1].ljust(max_args)}#{ANSI_RESET}"
print " #{ANSI_GRAY}#{item[2]}#{ANSI_RESET}\n"
end
data.size
end
def migrate
if defined? Rails::Console # turn off info logging for Rails 3
old_log_level = ActiveRecord::Base.logger.try(:sev_threshold)
ActiveRecord::Base.logger.sev_threshold = Logger::WARN
end
reload! && migations_ran = true if ActiveRecord::Migrator.migrate(Rails.root.join("db/migrate")).any?
ActiveRecord::Base.logger.sev_threshold = old_log_level if defined? old_log_level
migations_ran ||= nil # useful exit status
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment