From: Jonathan Steel on
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
--
Posted via http://www.ruby-forum.com/.

From: Robert Klemme on
On 07/23/2010 06:33 PM, Jonathan Steel wrote:
> 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

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
From: Jonathan Steel on
Robert Klemme wrote:
> On 07/23/2010 06:33 PM, Jonathan Steel wrote:
>> 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?

The aren't, just what I'm trying to do (below) requires an include,
except I want to use it 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?
>
> 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
--
Posted via http://www.ruby-forum.com/.