Skip to content

Instantly share code, notes, and snippets.

@maxsilver
Created June 2, 2010 17:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxsilver/422729 to your computer and use it in GitHub Desktop.
Save maxsilver/422729 to your computer and use it in GitHub Desktop.
Some methods to print pretty method lists
# Some methods to print pretty method lists
# The simple one is from Jason Vogel
# http://jasonvogel.blogspot.com/2007/01/ruby-code-snippet-methods-list.html
puts object.methods.sort.join("\n").to_s+"\n\n"
# This detailed one is from Conor Hunt
# http://snippets.dzone.com/posts/show/2916
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| "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].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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment