Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   Trace calls in a module (http://www.velocityreviews.com/forums/t863631-trace-calls-in-a-module.html)

Jonathan Steel 07-23-2010 04:33 PM

Trace calls in a module
 
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/.


Robert Klemme 07-23-2010 04:59 PM

Re: Trace calls in a module
 
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/

Jonathan Steel 07-23-2010 05:08 PM

Re: Trace calls in a module
 
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/.



All times are GMT. The time now is 07:53 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.