Skip to content

Instantly share code, notes, and snippets.

@fresh5447
Created February 20, 2014 20:41
Show Gist options
  • Select an option

  • Save fresh5447/9122775 to your computer and use it in GitHub Desktop.

Select an option

Save fresh5447/9122775 to your computer and use it in GitHub Desktop.
class Shape
attr_accessor :color
def initialize(color = nil)
@color = color || 'Red'
end
def larger_than?(other)
self.area > other.area
end
end
class Rectangle < Shape
attr_accessor :width, :height
def initialize(width, height, color = nil)
@width, @height = width, height
super(color)
end
def area
width * height
end
end
class Square < Rectangle
def initialize(side, color = nil)
super(side, side, color)
end
end
class Circle < Shape
attr_accessor :radius
def initialize(radius, color = nil)
@radius = radius
super(color)
end
def area
Math::PI * (radius * radius)
end
end
c = Circle.new(7, "Brown")
c2 = Circle.new(8)
p c.respond_to?('color') #why is this false?
p c.color
p c.larger_than?(c2)
p c.area
p c2.area
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment