faithfulgeek (owner)

Revisions

gist: 128362 Download_button fork
public
Public Clone URL: git://gist.github.com/128362.git
Embed All Files: show embed
exercise5.rb #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
module TraceCalls
 
    def following(*syms, &block)
      syms.each do |sym|
        hook_method = "__#{sym}__hooked__"
        self.class.send :alias_method, hook_method, sym
        #private hook_method
        self.class.send :define_method, sym do |*args|
          puts "==> Calling #{sym} with #{args.inspect}"
          result = send "__#{sym}__hooked__", *args
          puts "<== result #{result}"
        end
      end
  end
 
end
 
class Example
  include TraceCalls
 
  def some_method(arg1, arg2)
    arg1 + arg2
  end
end
 
ex = Example.new
ex.following :some_method
ex.some_method(4,5)