jedediah / print_members

Pretty print live methods/constants of an object/class in IRB

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

This is a handy introspection tool for use in IRB, or your REPL of choice. It pretty prints the following details about a class or module:

  • class inheritance chain

  • modules included in each class in the chain

  • nested modules and classes

  • nested constants

  • class methods

  • instance methods

  • singleton methods (if you pass it a non-module)

  • arity for each method or

  • formal parameter names (if it can find the method source)

  • methods grouped by lib/gem

It only works in Ruby 1.9 for now.

Examples (that look better in color)

>> PrintMembers.pm String
 String

  Superclasses     Included Modules
                   PrintMembers::Ext::String
                   Comparable
  Object           PrintMembers::Ext::Object
                   Kernel
  BasicObject      PrintMembers::Ext::BasicObject

 Class Methods:
  try_convert/1     

 Instance Methods:
  %/1                 concat/1            left_fixed(n,pad=?) squeeze!/-1        
  */1                 count/-1            length/0            start_with?/-1     
  +/1                 crypt/1             lines/-1            strip/0            
  ...
  • Classes are listed in order from most to least derived.

  • Modules appear next to the classes through which they are included.

  • If the source file for a method can be found, it is parsed to find the parameters for the method, otherwise the arity of the method is shown. Default arguments are not implemented yet and they currently appear as ‘?’.

If you pass an object to pm that is not a class or module, the class of that object will be used:

>> PrintMembers.pm 17
 Fixnum 

  Superclasses     Included Modules              
  Integer                                        
  Numeric          Comparable                    
  Object           PrintMembers::Ext::Object     
                   Kernel                        
  BasicObject      PrintMembers::Ext::BasicObject

 Instance Methods:
  %/1                      chr/-1                   polar/0                 
  &/1                      coerce/1                 pred/0                  
  */1                      conj/0                   quo/1                   
  ...

Passing a Regexp as the second argument will filter the methods:

>> PrintMembers.pm String, /to/
 String 

  Superclasses     Included Modules              
                   PrintMembers::Ext::String     
                   Comparable                    
  Object           PrintMembers::Ext::Object     
                   Kernel                        
  BasicObject      PrintMembers::Ext::BasicObject

 Instance Methods:
  to_c/0            to_f/0            to_r/0            to_str/0          upto/-1          
  to_color_string() to_i/-1           to_s/0            to_sym/0

The install method monkey patches Object with a pm method. Call it from your .irbrc file:

>> PrintMembers.install

>> Array.pm /^re/
Array 

 Superclasses     Included Modules              
                  PrintMembers::Ext::Array      
                  Stackable                     
                  Indexable                     
                  Enumerable                    
 Object           PrintMembers::Ext::Object     
                  Kernel                        
 BasicObject      PrintMembers::Ext::BasicObject

Instance Methods:
 recursively(&block)  reject/0             reverse/0            
 recursively!(&block) reject!/0            reverse!/0           
 reduce/-1            replace/1            reverse_each/0

Bonus features

lsrb [pattern]
module.lsrb [pattern]

Lists .rb files accessible from the load path, optionally filtering by regexp pattern. The second form lists only source files that define methods in module.

lslib [pattern]
module.lslib [pattern]

Like lsrb but identifies distinct libraries and gems (and gem versions) instead of listing all source files.

lsmod [pattern]
module.lsmod [pattern]

Lists modules and classes. The second form lists only nested modules under module.

Planned Features

  • ActiveRecord integration

  • Instance variables

  • Extract RDoc markup

  • Use RI database

  • View method source

  • More extensive module/library browsing

About

Pretty print live methods/constants of an object/class in IRB


Languages

Language:Ruby 100.0%