#!/usr/bin/ruby
module HookEachFunction
def setup_hooks(object)
object.instance_eval do
@orig_procs = Hash.new
object.public_methods(false).each { |method|
method_proc = object.method(method).to_proc
@orig_procs[method] = method_proc
eval("def " + method + "(*args)
puts 'before'
@orig_procs[\"" + method + "\"].call(args)
puts 'after'
end
")
}
end
end
end
class HookTest
include HookEachFunction
def initialize
setup_hooks(self)
end
def hi(to, you)
puts "hi" + to + you
end
def sup(dog)
puts dog
end
end
test = HookTest.new
test.hi(" to", " you")
test.sup("again")
outputs:
[jhobbs@pion ~]$ ./AutoTicker.rb
before
hi to you
after
before
again
after