# Trace calls in a module

**URL:** <https://www.ruby-forum.com/t/trace-calls-in-a-module/192097>\
**Category:** Ruby\
**Created:** [July 23, 2010, 6:33pm UTC](https://www.ruby-forum.com/t/trace-calls-in-a-module/192097 "2010-07-23T18:33:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![flamingsole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f19dbf/32.png) [@flamingsole](https://www.ruby-forum.com/u/flamingsole)\
**Post date:** [July 23, 2010, 6:33pm UTC](https://www.ruby-forum.com/t/trace-calls-in-a-module/192097/1 "2010-07-23T18:33:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![flamingsole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f19dbf/32.png) [@flamingsole](https://www.ruby-forum.com/u/flamingsole)\
**Post date:** [July 23, 2010, 7:05pm UTC](https://www.ruby-forum.com/t/trace-calls-in-a-module/192097/2 "2010-07-23T19:05:53Z")

</div>

On 07/23/2010 06:33 PM, Jonathan S. 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

---

<div class="post-metadata">

**Author:** ![flamingsole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f19dbf/32.png) [@flamingsole](https://www.ruby-forum.com/u/flamingsole)\
**Post date:** [July 23, 2010, 7:08pm UTC](https://www.ruby-forum.com/t/trace-calls-in-a-module/192097/3 "2010-07-23T19:08:02Z")

</div>

Robert K. wrote:

> On 07/23/2010 06:33 PM, Jonathan S. 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
