public
Created

This is my current irbrc file. Lots of helpful things I've collected over the years. Enjoy

  • Download Gist
.irbrc
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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.