Skip to content

Instantly share code, notes, and snippets.

@SandNerd
Last active March 2, 2016 00:48
Show Gist options
  • Save SandNerd/aa82fd205faf76abbc20 to your computer and use it in GitHub Desktop.
Save SandNerd/aa82fd205faf76abbc20 to your computer and use it in GitHub Desktop.
Meat transformed into a Burger using Ruby's subclassing, include and prepend
class Meat
def burger
puts "meat"
end
end
class SauceAndMeat < Meat
def burger
puts "sauce"
super # "meat"
end
end
module Lettuce
def burger
puts "lettuce"
super
end
end
module UpperBun
def burger
puts "upper_bun"
super
end
end
module LowerBun
def burger
super # notice a difference in ordering?
puts "lower_bun"
end
end
SauceAndMeat.include(LowerBun)
SauceAndMeat.include(Lettuce)
SauceAndMeat.prepend(UpperBun)
SauceAndMeat.new.burger
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment