Skip to content

Instantly share code, notes, and snippets.

@saterus
Created February 16, 2011 01:44
Show Gist options
  • Save saterus/828691 to your computer and use it in GitHub Desktop.
Save saterus/828691 to your computer and use it in GitHub Desktop.
Print Methods
# terminal color constants!
unless defined?(IRB_CONSTS_LOADED)
ANSI_BLACK = "\033[0;30m"
ANSI_GRAY = "\033[1;30m"
ANSI_LGRAY = "\033[0;37m"
ANSI_WHITE = "\033[1;37m"
ANSI_RED = "\033[0;31m"
ANSI_LRED = "\033[1;31m"
ANSI_GREEN = "\033[0;32m"
ANSI_LGREEN = "\033[1;32m"
ANSI_BROWN = "\033[0;33m"
ANSI_YELLOW = "\033[1;33m"
ANSI_BLUE = "\033[0;34m"
ANSI_LBLUE = "\033[1;34m"
ANSI_PURPLE = "\033[0;35m"
ANSI_LPURPLE = "\033[1;35m"
ANSI_CYAN = "\033[0;36m"
ANSI_LCYAN = "\033[1;36m"
ANSI_BACKBLACK = "\033[40m"
ANSI_BACKRED = "\033[41m"
ANSI_BACKGREEN = "\033[42m"
ANSI_BACKYELLOW = "\033[43m"
ANSI_BACKBLUE = "\033[44m"
ANSI_BACKPURPLE = "\033[45m"
ANSI_BACKCYAN = "\033[46m"
ANSI_BACKGRAY = "\033[47m"
ANSI_RESET = "\033[0m"
ANSI_BOLD = "\033[1m"
ANSI_UNDERSCORE = "\033[4m"
ANSI_BLINK = "\033[5m"
ANSI_REVERSE = "\033[7m"
ANSI_CONCEALED = "\033[8m"
XTERM_SET_TITLE = "\033]2;"
XTERM_END = "\007"
ITERM_SET_TAB = "\033]1;"
ITERM_END = "\007"
SCREEN_SET_STATUS = "\033]0;"
SCREEN_END = "\007"
IRB_CONSTS_LOADED = true
end
# best debugging method ever. provides on the fly documentation. you can basically grep for methods.
# usage: "test".pm Pretty Prints all methods without Object methods
# usage: pm("test", :more) Pretty Prints all methods with Object methods
# usage: pm("test", "^to_") Greps method names
# usage: pm("test", /(ActiveRecord)/) Greps method ancestors
# usage: pm("test", /(ActiveRecord)/, :n) Greps method ancestors, :n negates
class Object
def print_methods(obj=self, *options) # Print methods
methods = obj.methods
methods -= Object.methods unless options.include? :more
name_filter = options.detect {|opt| opt.kind_of? String}
if name_filter
name_filter = Regexp.new(name_filter)
methods = methods.select {|name| name =~ name_filter}
end
ancestor_filter = options.detect {|opt| opt.kind_of? Regexp}
if ancestor_filter
negated = options.detect {|opt| opt == :n }
ancestor_filter = /^((?!#{ ancestor_filter }).)*$/ if negated
methods = methods.select do |name|
method = obj.method(name)
ancestor_match = $1 if method.inspect =~ /Method: (.*?)#/
name unless (ancestor_match =~ ancestor_filter).nil?
end
end
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| "arg#{i}"}.join(", ")})"
elsif method.arity < 0
n = -method.arity
args = "(#{(1..n).collect {|i| "arg#{i}"}.join(", ")}, ...)"
end
method.inspect =~ /Method: ([^(]+)(\(.+\))?#/
klass_name = $1
ancestor = $2
ancestor = nil if method.inspect =~ /#<Module:0x[^>]+>/
klass = [klass_name, ancestor].compact.join
[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}#{ANSI_RED}#{item[0].to_s.rjust(max_name)}#{ANSI_RESET}"
print "#{ANSI_GREEN}#{item[1].to_s.ljust(max_args)}#{ANSI_RESET}"
print " #{ANSI_CYAN}#{item[2].to_s}#{ANSI_RESET}\n"
end
data.size
end
alias :pm :print_methods
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment