Never been to DZone Snippets before?

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 (See related posts)

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

   1  
   2      ANSI_BOLD       = "\033[1m"
   3      ANSI_RESET      = "\033[0m"
   4      ANSI_LGRAY    = "\033[0;37m"
   5      ANSI_GRAY     = "\033[1;30m"
   6  
   7      def pm(obj, *options) # Print methods
   8        methods = obj.methods
   9        methods -= Object.methods unless options.include? :more
  10        filter = options.select {|opt| opt.kind_of? Regexp}.first
  11        methods = methods.select {|name| name =~ filter} if filter
  12  
  13        data = methods.sort.collect do |name|
  14          method = obj.method(name)
  15          if method.arity == 0
  16            args = "()"
  17          elsif method.arity > 0
  18            n = method.arity
  19            args = "(#{(1..n).collect {|i| "arg#{i}"}.join(", ")})"
  20          elsif method.arity < 0
  21            n = -method.arity
  22            args = "(#{(1..n).collect {|i| "arg#{i}"}.join(", ")}, ...)"
  23          end
  24          klass = $1 if method.inspect =~ /Method: (.*?)#/
  25          [name, args, klass]
  26        end
  27        max_name = data.collect {|item| item[0].size}.max
  28        max_args = data.collect {|item| item[1].size}.max
  29        data.each do |item| 
  30          print " #{ANSI_BOLD}#{item[0].rjust(max_name)}#{ANSI_RESET}"
  31          print "#{ANSI_GRAY}#{item[1].ljust(max_args)}#{ANSI_RESET}"
  32          print "   #{ANSI_LGRAY}#{item[2]}#{ANSI_RESET}\n"
  33        end
  34        data.size
  35      end

Comments on this post

nikosd posts on Apr 17, 2008 at 10:37
First of all thanks a lot for this snippet!

Ok, here's a better version. Ruby is OO, right? ;)

   1  
   2  class Object
   3    ANSI_BOLD       = "\033[1m"
   4    ANSI_RESET      = "\033[0m"
   5    ANSI_LGRAY    = "\033[0;37m"
   6    ANSI_GRAY     = "\033[1;30m"
   7  
   8    # Print object's methods
   9    def pm(*options)
  10      methods = self.methods
  11      methods -= Object.methods unless options.include? :more
  12      filter = options.select {|opt| opt.kind_of? Regexp}.first
  13      methods = methods.select {|name| name =~ filter} if filter
  14  
  15      data = methods.sort.collect do |name|
  16        method = self.method(name)
  17        if method.arity == 0
  18          args = "()"
  19        elsif method.arity > 0
  20          n = method.arity
  21          args = "(#{(1..n).collect {|i| "arg#{i}"}.join(", ")})"
  22        elsif method.arity < 0
  23          n = -method.arity
  24          args = "(#{(1..n).collect {|i| "arg#{i}"}.join(", ")}, ...)"
  25        end
  26        klass = $1 if method.inspect =~ /Method: (.*?)#/
  27        [name, args, klass]
  28      end
  29      max_name = data.collect {|item| item[0].size}.max
  30      max_args = data.collect {|item| item[1].size}.max
  31      data.each do |item| 
  32        print " #{ANSI_BOLD}#{item[0].rjust(max_name)}#{ANSI_RESET}"
  33        print "#{ANSI_GRAY}#{item[1].ljust(max_args)}#{ANSI_RESET}"
  34        print "   #{ANSI_LGRAY}#{item[2]}#{ANSI_RESET}\n"
  35      end
  36      data.size
  37    end
  38  end


Now you can happily hit 1.pm

Cheers!

You need to create an account or log in to post comments to this site.


Click here to browse all 5349 code snippets

Related Posts