Skip to content

Instantly share code, notes, and snippets.

@ivorpad
Last active August 29, 2015 14:24
Show Gist options
  • Select an option

  • Save ivorpad/a0308b67728883b90144 to your computer and use it in GitHub Desktop.

Select an option

Save ivorpad/a0308b67728883b90144 to your computer and use it in GitHub Desktop.
More Complicated Inheritance: Bloc.io
class Shape
attr_accessor :color
def initialize(color = nil)
@color = color || 'Red'
end
def larger_than?(shapeType)
if self.area >= shapeType.area
true
else
false
end
end
end
class Rectangle < Shape
attr_accessor :width, :height
def initialize(width, height, color = nil)
@width, @height = width, height
super(color) # this calls Shape#initialize
end
def area
@width * @height
end
end
class Square < Rectangle
def initialize(side, color = nil)
super(side, side, color) # this calls Shape#initialize
end
end
class Circle < Shape
attr_accessor :radius
def initialize(radius, color = nil)
@radius = radius
super(color) # this calls Shape#initialize
end
def area
Math::PI * (@radius * @radius)
end
end
c = Circle.new(3)
p c.area
s = Square.new(13)
puts s.color
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment