Created
March 31, 2015 15:00
-
-
Save capitalist/473543b7ea24d461d030 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
#!/usr/bin/env ruby | |
def Inherits(*classes) | |
klass = Class.new classes.shift # inherit from the last clas | |
while refine_with = classes.shift # include the others as module refinements just so we can turn them into modules | |
klass.include Module.new { include refine(refine_with) { } } | |
end | |
klass | |
end | |
class TwoD | |
attr_accessor :width, :height | |
def area | |
width * height | |
end | |
end | |
class ThreeD | |
attr_accessor :depth | |
def area | |
# why can't i call super here | |
# this method is included in the generated base class for Cube via the anonymous mixin which includes the ThreeD refinement | |
width * height * depth | |
end | |
end | |
class Square < TwoD | |
def width= w | |
super | |
self.height = w | |
end | |
end | |
class Cube < Inherits(Square,ThreeD) | |
def width= w | |
super | |
self.depth = w | |
end | |
def area | |
super | |
end | |
end | |
s = Square.new | |
s.width = 4 | |
puts "Square Area: #{s.area}" | |
c = Cube.new | |
c.width = 4 | |
puts "Cube Area: #{c.area}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
multiple inheritance in ruby, cool!
apart from the fact that you are returning the cube volume, not the area ;)