Created
August 7, 2009 22:45
-
-
Save takai/164212 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
module AOP | |
def before_advice target_method, &advice | |
original_method = :"#original_{sym}" | |
self.class.class_eval do | |
alias_method original_method, target_method | |
define_method target_method do | |
advice.call self | |
__send__ original_method | |
end | |
end | |
end | |
end | |
array = [2, 3, 1] | |
array.extend AOP | |
array.before_advice :sort do |obj| | |
p "before sort: #{obj.inspect}" | |
end | |
array.sort |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment