0
0
Rubyprogramming~15 mins

Comments and documentation in Ruby - Mini Project: Build & Apply

Choose your learning style9 modes available
Comments and documentation
📖 Scenario: You are working on a small Ruby program that calculates the area of rectangles. To make your code easier to understand for others and for your future self, you will add comments and documentation step-by-step.
🎯 Goal: Create a Ruby program that stores rectangle dimensions, adds comments explaining the code, and prints the area with clear documentation.
📋 What You'll Learn
Create a hash with rectangle dimensions
Add a comment explaining the hash
Write a method with a documentation comment
Print the area with a descriptive comment
💡 Why This Matters
🌍 Real World
Adding comments and documentation helps teams understand code quickly and maintain it easily over time.
💼 Career
Clear comments and documentation are essential skills for software developers to communicate their code intent and logic.
Progress0 / 4 steps
1
Create the rectangle dimensions hash
Create a hash called rectangle with these exact keys and values: :width set to 10 and :height set to 5.
Ruby
Need a hint?

Use Ruby hash syntax with symbols for keys, like { width: 10, height: 5 }.

2
Add a comment explaining the rectangle hash
Add a comment above the rectangle hash that says: # This hash stores the width and height of the rectangle.
Ruby
Need a hint?

Comments in Ruby start with #. Place it on the line before the hash.

3
Write a method with a documentation comment
Write a method called area that takes one parameter rect. Add a comment above it that says: # Calculates the area of a rectangle given its dimensions. The method should return the product of rect[:width] and rect[:height].
Ruby
Need a hint?

Define the method with def area(rect) and multiply the width and height inside.

4
Print the area with a descriptive comment
Add a comment that says # Print the area of the rectangle and then write a puts statement that prints the result of calling area(rectangle).
Ruby
Need a hint?

Use puts area(rectangle) to print the area. The comment should be right above it.