3
我所遇到下面的代碼:define_method是否重寫與其參數具有相同名稱的方法?
class MethodLogger
def log_method((klass,method_name)
klass.class_eval do
alias_method "#{method_name}_original" method_name
define_method method_name do
puts "#{Time.now}: Called #{method_name}"
send "#{method_name}_original"
end
end
end
end
class Tweet
def say_hi
puts "Hi"
end
end
logger =MethodLogger.new
logger.log_method(Tweet,:say_hi)
這使輸出
2012-09-11 12:54:09 -400: Called say_hi
那麼,如何在define_method:say_hi覆蓋原始方法:say_hi?或者define_method改變了原來的方法定義?