Created
August 6, 2012 12:21
-
-
Save shime/3274039 to your computer and use it in GitHub Desktop.
overriding instance method with module, ultimate solution
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 Prepending | |
def append_features(base) | |
prepend = self | |
base.extend Module.new { define_method(:new) { |*args,&block| super(*args,&block).extend(prepend) }} | |
end | |
end | |
module CanCook | |
extend Prepending | |
def bake(food_name) | |
"I'm baking the #{food_name}!" | |
end | |
end | |
class Programmer | |
def bake(food_name) | |
"I don't know how to bake #{food_name} :(" | |
end | |
end | |
class Programmer | |
include CanCook | |
end | |
Mario = Programmer.new | |
Mario.bake("tortillas") #=> I'm baking the tortillas! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment