Hi guys, I'm having problems with using alias_method. When i run this
code, I'm getting undefined method `shout' for class `Person'
(NameError) since I'm using alias_method on a function that is probably
not defined yet. The example would work if I put the profile method
after the method name, but is there a cleaner solution to make this
work? This post (http://www.ruby-forum.com/topic/100587) seem to solve
this problem using "method_added" callback but i hope i could use a
different approach
module SimpleProfiler
def profile(*methods)
methods.each do |method|
send :alias_method, "orig_#{method}", method
define_method method do |name|
before = Time.now
send "orig_#{method}", name
after = Time.now
puts sprintf("%s: %.4fs", method, after - before)
end
end
end
end
class Person
extend SimpleProfiler
profile :shout
def shout(name)
sleep(1)
puts name
end
end
p = Person.new
p.shout("reg")
···
--
Posted via http://www.ruby-forum.com/.