Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Pry is amazing for understanding code. IMO the most useful method is absolutely `some_object.methods`, but you can also improve the output a ton by always doing:

    some_object.methods - {}.methods # or some other "parent" class/object
This does set subtraction to remove all the "core" object methods from your some_object methods, so you can see just what's unique about that object. For example, comparing one of my services with a parent Service class it inherits from:

    irb(main):017:0> PaypalService.methods - Service.methods
    => [:capture_invoice_funds, :months_price, :client, :create_prepay_invoice, :order_info]
instead of listing out the 187 methods that PaypalService has that it inherits from Service and every other parent object up the line, which is usually a surprisingly long ancestor list in any Ruby on Rails class (even POROs!).

There are a TON of useful methods like this that I'd recommend getting familiar with, which makes exploration and discovery of code incredibly fast and efficient. You can find them all with the same trick: by using `methods` on any method. :)

    irb(main):043:0> PaypalService.method(:months_price).methods - {}.methods
    => 
    [:<<,                                                                                             
    :>>,                                                                                             
    :arity,                                                                                          
    :curry,                                                                                          
    :source_location,                                                                                
    :call,                                                                                           
    :parameters,                                                                                     
    :original_name,                                                                                  
    :owner,                                                                                          
    :receiver,                                                                                       
    :super_method,                                                                                   
    :name,                                                                                           
    :unbind,                                                                                         
    :comment,                                                                                        
    :source]
Some of the more relevant ones:

    irb(main):037:0> PaypalService.method(:months_price).arity
    => 1

    irb(main):038:0> puts PaypalService.method(:months_price).parameters
    req
    n_months                                                                                  

    irb(main):039:0> puts PaypalService.method(:months_price).source
      def self.months_price(n_months)
        case n_months                                                                         
        when 1                                                                                
          9.00                                                                                
        when 3                                                                                
          24.00
        when 6
          48.00
        when 12
          84.00
        else
          raise "Invalid month prepay: #{n_months}"
        end
      end


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: