Class: Object

Inherits:
BasicObject
Defined in:
lib/puppy.rb

Overview

This is where the magic happens, open the Object class and define the new #trace instance method for every object instances.

Instance Method Summary collapse

Instance Method Details

#trace(options = {}, &block) ⇒ Object

Starts tracing the object instance and prints every call to its instance methods with given arguments. Can be invoked with a code block to perform conditional tracing, original object instance, method and arguments list are passed to the code block.

Important Note:

If you assign the traced variable to another object which is not traced, you will lose the ability to trace the new instance unless you call the #trace method explicitly.

Examples:

# The following will print:
#    Fixnum.to_ary() [puppy.rb:71:in `puts'] 
#    Fixnum.respond_to?(:to_ary) [puppy.rb:71:in `puts'] 
#    Fixnum.to_s() [puppy.rb:71:in `puts'] 
#    1
foo = 1.trace
puts foo

# Trace the object without reporting the caller and represent 
# it using its variable name instead of its #class, will print:
# # bar.to_ary() 
# # bar.respond_to?(:to_ary) 
# # bar.to_s()

  # something

bar = "something".trace :as => 'bar', :caller => false
puts bar

# Trace only when arguments list is filled with something.
count = 3.trace { |object,method,*args| args.size > 0 }
puts count.size    # this won't be traced
puts count.to_s(2) # this will

Options:

  • :as - how to represent the instance as string, if a Symbol is given it will be invoked as a method, otherwise as a string. Default: :class

  • :caller - a boolean to enable or disable printing caller data. Default: true

  • :step - if true stop the execution on each method invoked and wait for the user to press a key. Default: false

  • :indent - if true method invocations will be indented as their nesting goes deeper. Default: true

  • :stream - the stream to use to print the report while tracing the object, must overload the << operator. Default: STDERR



46
47
48
# File 'lib/puppy.rb', line 46

def trace( options = {}, &block )
	Puppy::TracedObject.new( self, options, &block )
end