Skip to content

Instantly share code, notes, and snippets.

@bakineggs
Created March 5, 2011 10:46
Show Gist options
  • Select an option

  • Save bakineggs/856288 to your computer and use it in GitHub Desktop.

Select an option

Save bakineggs/856288 to your computer and use it in GitHub Desktop.
class Module
def trace_callers method
alias_method :"#{method}_without_trace", method
define_method method do |*args, &blk|
puts "Invocation of #{method}:"
puts caller.map {|line| " #{line}"}
send :"#{method}_without_trace", *args, &blk
end
end
end
require 'trace_callers'
class Foo
trace_callers :initialize
def bar
yield
end
trace_callers :bar
end
def outer
inner Foo.new
end
def inner foo
foo.bar { puts "Cool!" }
end
outer
# Ouputs the following:
#
# Invocation of initialize:
# trace_callers_example.rb:13:in `new'
# trace_callers_example.rb:13:in `outer'
# trace_callers_example.rb:20
# Invocation of bar:
# trace_callers_example.rb:17:in `inner'
# trace_callers_example.rb:13:in `outer'
# trace_callers_example.rb:20
# Cool!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment