I have come across the following code :
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)
This gives output
2012-09-11 12:54:09 -400: Called say_hi
So, how does the define_method :say_hi override the original method :say_hi ? Or does define_method change the original method definition ?
If you define a method multiple times, all latter definitions will simply overwrite the older ones. There can only be one method with the same name.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With