Skip to content

Instantly share code, notes, and snippets.

@chronicole
Created March 27, 2011 18:49
Show Gist options
  • Save chronicole/889464 to your computer and use it in GitHub Desktop.
Save chronicole/889464 to your computer and use it in GitHub Desktop.
my customized ~./irbrc file
# Nicole's Code or Code Not custom IRB file
# http://codeorcodenot.blogspot.com/2011/03/setting-up-house-creating-custom-irbrc.html
# References:
# http://www.alloycode.com/2010/6/26/automatic-color-coding-for-script-console-and-irb
# http://robots.thoughtbot.com/post/159806033/irb-script-console-tips
# http://drnicwilliams.com/2006/10/12/my-irbrc-for-consoleirb/
# http://www.dotnetguy.co.uk/post/2011/03/27/ruby-syntax-highlighting-within-the-console-with-wirble-and-the-rvm
# -------------------------------------------------------------------
# Loads the following gems for IRB
# 8.27.11 NOTE - unless Gem.available? is deprecated but haven't sorted out how to use the replacement Gem::Specification::find_by_name yet
# -------------------------------------------------------------------
require "rubygems"
%x{gem install 'awesome_print' --no-ri --no-rdoc} unless Gem.available?('awesome_print')
Gem.refresh # if you don't already have awesome print gem installed it looks for it and refreshes before requiring
require "ap"
%x{gem install 'what_methods' --no-ri --no-rdoc} unless Gem.available?('what_methods')
Gem.refresh # if you don't already have what methods gem installed it looks for it and refreshes before requiring
require "what_methods"
# -------------------------------------------------------------------
# writing quit wears me out
# -------------------------------------------------------------------
alias q exit
# -------------------------------------------------------------------
# Build a simple colorful IRB prompt
# -------------------------------------------------------------------
ANSI = {}
ANSI[:RESET] = "\e[0m"
ANSI[:BOLD] = "\e[1m"
ANSI[:UNDERLINE] = "\e[4m"
ANSI[:LGRAY] = "\e[0;37m"
ANSI[:GRAY] = "\e[0;90m"
ANSI[:RED] = "\e[31m"
ANSI[:GREEN] = "\e[32m"
ANSI[:YELLOW] = "\e[33m"
ANSI[:BLUE] = "\e[34m"
ANSI[:MAGENTA] = "\e[35m"
ANSI[:CYAN] = "\e[36m"
ANSI[:WHITE] = "\e[37m"
IRB.conf[:PROMPT][:SIMPLE_COLOR] = {
:PROMPT_I => "#{ANSI[:BLUE]}>>#{ANSI[:RESET]} ",
:PROMPT_N => "#{ANSI[:CYAN]}>>#{ANSI[:RESET]} ",
:PROMPT_C => "#{ANSI[:RED]}?>#{ANSI[:RESET]} ",
:PROMPT_S => "#{ANSI[:YELLOW]}?>#{ANSI[:RESET]} ",
:RETURN => "#{ANSI[:GREEN]}=>#{ANSI[:RESET]} %s\n",
:AUTO_INDENT => true,
:USE_READLINE => true }
IRB.conf[:PROMPT_MODE] = :SIMPLE_COLOR
# -------------------------------------------------------------------
# Lists the methods and what arguments they take on any given object, can be passed regex arg
# ex. some_string = "foobar"
# pm some_string (without regex)
# pm some_string, /up/ (with regex)
# -------------------------------------------------------------------
class Object
ANSI_BOLD = "\033[1m"
ANSI_RESET = "\033[0m"
ANSI_LGRAY = "\033[0;37m"
ANSI_GRAY = "\033[1;30m"
# Print object's methods
def pm(*options)
methods = self.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 = self.method(name)
if method.arity == 0
args = "()"
elsif method.arity > 0
n = method.arity
args = "(#{(1..n).collect {|i| "arg#{i}"}.join(", ")})"
elsif method.arity < 0
n = -method.arity
args = "(#{(1..n).collect {|i| "arg#{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].to_s.rjust(max_name)}#{ANSI_RESET}"
print "#{ANSI_GRAY}#{item[1].to_s.ljust(max_args)}#{ANSI_RESET}"
print " #{ANSI_LGRAY}#{item[2]}#{ANSI_RESET}\n"
end
data.size
end
end
# -------------------------------------------------------------------
# Awesome Print
# Pretty print your Ruby objects with style -- in full color and with proper indentation
# -------------------------------------------------------------------
unless IRB.version.include?('DietRB')
IRB::Irb.class_eval do
def output_value
ap @context.last_value,
:multiline => false,
:plain => false,
:indent => 2,
:color => {
:array => :white,
:bignum => :blue,
:class => :yellow,
:date => :greenish,
:falseclass => :red,
:fixnum => :blue,
:float => :blue,
:hash => :gray,
:nilclass => :red,
:string => :yellowish,
:symbol => :cyanish,
:time => :greenish,
:trueclass => :green
}
end
end
else # MacRuby
IRB.formatter = Class.new(IRB::Formatter) do
def inspect_object(object)
object.ai
end
end.new
end
# -------------------------------------------------------------------
# Lets me see only non trivial methods in a sane order on any class or instance I'm exploring
# -------------------------------------------------------------------
class Object
# Return only the methods not present on basic objects
def interesting_methods
self.methods false
end
end
# -------------------------------------------------------------------
# You done good kid
# -------------------------------------------------------------------
puts "> You are ready to rock the IRB <"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment