Trace calls in a module

Discussion in 'Ruby' started by Jonathan Steel, Jul 23, 2010.

  1. 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/.
    Jonathan Steel, Jul 23, 2010
    #1
    1. Advertising

  2. 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/
    Robert Klemme, Jul 23, 2010
    #2
    1. Advertising

  3. 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/.
    Jonathan Steel, Jul 23, 2010
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rukmal Fernando
    Replies:
    4
    Views:
    852
    Richard Grimes [MVP]
    Oct 27, 2003
  2. Matthias S.
    Replies:
    1
    Views:
    651
    Daniel Fisher\(lennybacon\)
    Nov 30, 2005
  3. Jiho Han
    Replies:
    0
    Views:
    519
    Jiho Han
    Aug 22, 2006
  4. =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?=

    Adding my messages to the trace.axd trace?

    =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?=, Apr 6, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    595
    =?Utf-8?B?RGF2aWQgVGhpZWxlbg==?=
    Apr 6, 2007
  5. Chris Newby
    Replies:
    0
    Views:
    768
    Chris Newby
    Jul 28, 2008
Loading...

Share This Page