Exercise 1: Make a Rectangle Class

Implement a small class representing the rectangle shape.

We'll cover the following

Problem statement

Write a Rectangle class with height and width attributes and an area() instance method.

  • Populate these attributes through the initialize(height, width) method.
  • Define attribute readers for these attributes.


With your definition, consider the following code:

rect = Rectangle.new(5,10)
puts rect.height
puts rect.width
puts rect.area()

It’s expected to print:


Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy