Created
August 21, 2013 11:13
-
-
Save dekart/6293194 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
class Object | |
def self.my_decorator(method_name) | |
alias_method "#{method_name}_without_my_decorator", method_name | |
define_method method_name do | |
puts "My decorator logic starts" | |
result = send("#{method_name}_without_my_decorator") | |
puts "My decorator logic ends" | |
result | |
end | |
end | |
end | |
class MyClass | |
def hello | |
puts "Hello world!" | |
123 | |
end | |
my_decorator :hello | |
end | |
MyClass.new.hello | |
# My decorator logic starts | |
# Hello world! | |
# My decorator logic ends | |
# => 123 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment