Skip to content

Instantly share code, notes, and snippets.

@codeprimate
Forked from shime/cooks.rb
Created August 7, 2012 07:03
Show Gist options
  • Save codeprimate/3282595 to your computer and use it in GitHub Desktop.
Save codeprimate/3282595 to your computer and use it in GitHub Desktop.
overriding instance method with module, ultimate solution
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