Created
May 11, 2014 06:11
-
-
Save amclain/1c4d9c5e5b022e8c5324 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 DecoratorExample | |
# Add functionality to a method. | |
def self.my_decorator method | |
imeth = instance_method method | |
define_method method do |*args, &block| | |
"does stuff to " + | |
imeth.bind(self).call(*args, &block) | |
end | |
end | |
# Returns a description of itself. | |
my_decorator def foo | |
'the foo method' | |
end | |
end | |
d = DecoratorExample.new | |
puts d.foo | |
# => does stuff to the foo method | |
require 'pry'; binding.pry |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment