Trace calls in a module

J

Jonathan Steel

How do you do the equivalent of include in a module? I would like to
trace all the calls in a module. There is an example for how to do this
as a mixin for a class in the pick axe, but how do I do it for a module?
Can I get access to the meta class for the module and mix it in there?

Thanks
 
R

Robert Klemme

How do you do the equivalent of include in a module? I would like to
trace all the calls in a module.

I don't understand this: why is tracing of method calls equivalent to
module inclusion?
There is an example for how to do this
as a mixin for a class in the pick axe, but how do I do it for a module?
Can I get access to the meta class for the module and mix it in there?

I don't have my pickaxe handy right now. What does the code do? It may
be possible to just exchange a module with the class.

Kind regards

robert
 
J

Jonathan Steel

Robert said:
I don't understand this: why is tracing of method calls equivalent to
module inclusion?

The aren't, just what I'm trying to do (below) requires an include,
except I want to use it in a module.
I don't have my pickaxe handy right now. What does the code do? It may
be possible to just exchange a module with the class.

Kind regards

robert

module TraceCalls
def self.included(klass)
klass.instance_methods(false).each do |existing_method|
wrap(klass, existing_method)
end
def klass.method_added(method)
unless @trace_calls_internal
@trace_calls_internal = true
TraceCalls.wrap(self, method)
@trace_calls_internal = false
end
end
def self.wrap(klass, method)
klass.instance_eval do
method_object = instance_method(method)
define_method(method) do |*args, &block|
puts "==> calling #{method} with #{args.inspect}"
result = method_object.bind(self).call(*args, &block)
puts "<== #{method} returned #{result.inspect}"
result
end
end
end
end
end
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top