DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Ruby IRB Helper Method To Pretty-print Object Methods
A simple method to print out the methods for an object at run time in a nicely formatted and colorized way. Really useful when using irb
Place in your .irbrc for easy usage, example here:
http://dotfiles.org/~sd/.irbrc
usage:
pm object
pm object, :more - shows all methods including base Object methods
pm object, :more, /to/ - shows all methods filtered by regexp
Coded by sebastian delmont
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






Comments
Snippets Manager replied on Sun, 2011/11/27 - 6:17am
### Request Interactive Method List ### # Regular expression argument denotes list request. String.ri // "".ri // # Show method names matching a regular expression. "".ri /case/ "".ri /^to_/ [].ri /sort/ {}.ri /each/ # Show ALL methods, including those private of Kernel. Hash.ri //, :all => true Hash.ri //, :all # Show class methods or instance methods only. Module.ri //, :access => "::" Module.ri //, :access => "#" # Show own methods only. Time.ri //, :own => true Time.ri //, :own # Specify visibility: public, protected or private. Module.ri //, :visibility => :private Module.ri //, :visibility => [:public, :protected] # Filter fully formatted name by given regexp. Module.ri //, :fullre => /\(Object\)::/ # Combine options. Module.ri //, :fullre => /\(Object\)::/, :access => "::", :visibility => :private ### Request RI on a Class ### Array.ri String.ri [].ri "".ri 5.ri ### Request RI on a Method ### String.ri :upcase "".ri :upcase [].ri :sort Hash.ri :[] Hash.ri "::[]" Hash.ri "#[]"Cheers!Snippets Manager replied on Thu, 2008/04/17 - 10:34am
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].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 endNow you can happily hit 1.pm Cheers!