Created
April 26, 2012 09:11
-
-
Save makaroni4/2497938 to your computer and use it in GitHub Desktop.
Wrap instance methods of object to log them
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User | |
def say | |
puts "Hello" | |
end | |
end | |
def override_instance_methods(c) | |
c.instance_methods(false).each do |m| | |
c.class_eval <<-RUBY | |
alias #{m}_original #{m} | |
def #{m}(*args, &block) | |
puts "Called #{c.name}##{m}" | |
#{m}_original(*args, &block) | |
end | |
RUBY | |
end | |
end | |
override_instance_methods User | |
User.new.say | |
# => Called User#say | |
# => Hello |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment