Skip to content

Instantly share code, notes, and snippets.

@alexhawkins
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexhawkins/3a95f9f8589f879ed767 to your computer and use it in GitHub Desktop.
Save alexhawkins/3a95f9f8589f879ed767 to your computer and use it in GitHub Desktop.
Why use self.width in the area method instead of @width?
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
self.width * self.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 * self.radius**2
end
end
@alexhawkins
Copy link
Author

TEST WITH:
a= Rectangle.new(1, 2, "Blue")
b= Square.new(5, "Green")
c = Circle.new(7, "Brown")
p c.area
p b.larger_than?(a)
p a.larger_than?(b)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment