Skip to content

Instantly share code, notes, and snippets.

@imajes
Created April 6, 2011 15:34
Show Gist options
  • Save imajes/905865 to your computer and use it in GitHub Desktop.
Save imajes/905865 to your computer and use it in GitHub Desktop.
# Tab Completion
require "irb/completion"
require "rubygems"
# from https://github.com/aniero/dotfiles/blob/master/irbrc
# Assume a bundler-managed environment, so find and require the irb-specific
# enhancement gems directly. This does not do dependency tracking, so
# dependent gems must be explicitly specified!
def irb_require(gemname, lib=nil)
# allow bundler or rubygems a fair shake at loading the library first
require lib ? lib : gemname
rescue LoadError
# didn't work? we'll do it ourself
candidates = []
p ENV["GEM_PATH"]
ENV["GEM_PATH"].split(":").map {|p| p + "/gems" }.each do |path|
Dir.glob(path + "/*").each do |entry|
if File.directory?(entry) && File.basename(entry).start_with?(gemname)
puts "entry: #{entry}"
candidates << entry
end
end
end
if candidates.empty?
raise LoadError, "could not load #{lib || gemname} via irb_require"
else
puts "manipulating load path for #{gemname}"
$:.push(candidates.sort.reverse.first + "/lib")
require lib ? lib : gemname
end
end
##
## needed for bundler, for console
#group :console do
# gem 'wirble'
# gem 'sketches'
# gem 'hirb'
# gem 'looksee'
# gem 'net-http-spy'
# gem 'awesome_print'
# gem 'what_methods'
#end
##
# Dr Nic's gem inspired by
# http://redhanded.hobix.com/inspect/stickItInYourIrbrcMethodfinder.html
#irb_require 'what_methods'
irb_require 'awesome_print', 'ap'
# Print information about any HTTP requests being made
irb_require 'net-http-spy'
# Wirble is a set of enhancements for irb
# http://pablotron.org/software/wirble/README
# Implies require 'pp', 'irb/completion', and 'rubygems'
irb_require 'wirble'
Wirble.init
# Enable colored output
Wirble.colorize
## LOAD ORDER IS IMPORTANT HERE
# Draw ASCII tables
irb_require 'hirb'
irb_require 'hirb/import_object'
Hirb.enable
extend Hirb::Console
# 'lp' to show method lookup path
#irb_require 'looksee'
# make overridden methods purple instead of black
#Looksee.styles[:overridden] = "\e[1;35m%s\e[0m"
# Load the readline module.
IRB.conf[:USE_READLINE] = true
# Remove the annoying irb(main):001:0 and replace with >>
IRB.conf[:PROMPT_MODE] = :SIMPLE
# Automatic Indentation
IRB.conf[:AUTO_INDENT]=true
# Save History between irb sessions
require 'irb/ext/save-history'
IRB.conf[:SAVE_HISTORY] = 100
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb-history"
# Clear the screen
def clear
system 'clear'
if ENV['RAILS_ENV']
return "Rails environment: " + ENV['RAILS_ENV']
else
return "No rails environment - happy hacking!";
end
end
#alias_method :clear, :clr
# quick quit
def q
quit
end
# Load / reload files faster
# http://www.themomorohoax.com/2009/03/27/irb-tip-load-files-faster
def fl(file_name)
file_name += '.rb' unless file_name =~ /\.rb/
@@recent = file_name
load "#{file_name}"
end
def rl
fl(@@recent)
end
# Reload the file and try the last command again
# http://www.themomorohoax.com/2009/04/07/ruby-irb-tip-try-again-faster
def rt
rl
eval(choose_last_command)
end
# prevent 'rt' itself from recursing.
def choose_last_command
real_last = Readline::HISTORY.to_a[-2]
real_last == 'rt' ? @@saved_last : (@@saved_last = real_last)
end
# Method to pretty-print object methods
# Coded by sebastian delmont
# http://snippets.dzone.com/posts/show/2916
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].ljust(max_args)}#{ANSI_RESET}"
print " #{ANSI_LGRAY}#{item[2]}#{ANSI_RESET}\n"
end
data.size
end
end
# http://sketches.rubyforge.org/
require 'sketches'
Sketches.config :editor => ENV['EDITOR']
# Easily print methods local to an object's class
class Object
def local_methods
(methods - Object.instance_methods).sort
end
end
# Log to STDOUT if in Rails
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