I am new to Ruby and I'm working on an exercise about Classes and Inheritance.
The instructions are as follows:
We should be able to call a larger_than? method on each shape. This method should evaluate two shapes and return true or false depending on one shape's area being larger than the other. In other words, the larger_than? method should return true if the receiving object is larger than the argument object:
I am having problems creating the larger_than? method. I don't know how to get the area from the other shapes and compare it to the receiving object's area.
Here's my code:
class Shape attr_accessor :color, :area def initialize(color = nil) @color = color || 'Red' end def larger_than?(object_area) if self.class.area > object_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) # calls `Rectangle#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
I don't understand why I'm facing this error:
NoMethodError undefined method `larger_than?' for #<B:0x007fdf3047d5a8 @color="Red">