📖 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
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
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
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
Hint
Use puts area(rectangle) to print the area. The comment should be right above it.
Practice
(1/5)
1. What symbol is used to start a single-line comment in Ruby?
easy
A. #
B. //
C.
D. /*
Solution
Step 1: Identify single-line comment syntax in Ruby
Ruby uses # to start a single-line comment.
Step 2: Compare with other options
Options A, B, and C are comment symbols from other languages (JavaScript, HTML, C-style).
Final Answer:
# -> Option A
Quick Check:
Single-line comment = # [OK]
Hint: Single-line comments start with # in Ruby [OK]
Common Mistakes:
Confusing Ruby comments with C-style or HTML comments
Using // instead of #
Trying to use /* */ for single-line comments
2. Which of the following is the correct way to write a multi-line comment in Ruby?
easy
A. //
This is a multi-line comment
//
B. /*
This is a multi-line comment
*/
C.
D. =begin
This is a multi-line comment
=end
Solution
Step 1: Recall Ruby multi-line comment syntax
Ruby uses =begin and =end to mark multi-line comments.
Step 2: Check other options
Options B, C, and D are multi-line comment styles from other languages (C, HTML, JavaScript).
Final Answer:
=begin
This is a multi-line comment
=end -> Option D
Quick Check:
Multi-line comment = =begin ... =end [OK]
Hint: Use =begin and =end for multi-line comments in Ruby [OK]
Common Mistakes:
Using /* */ which is not valid in Ruby
Using HTML style comments
Trying to use // for multi-line comments
3. What will be the output of this Ruby code?
def greet(name)
# This method returns a greeting message
"Hello, #{name}!"
end
puts greet("Alice")
medium
A. greet
B. # This method returns a greeting message
C. Hello, Alice!
D. SyntaxError
Solution
Step 1: Understand the method and comment
The comment line is ignored by Ruby and does not affect output. The method returns "Hello, #{name}!" with name replaced.
Step 2: Check the output of puts greet("Alice")
It prints the string "Hello, Alice!" to the console.
=begin
This is a multi-line comment
# Another comment line
=end
puts "Done"
medium
A. =begin and =end must be at the start of the line
B. The # inside multi-line comment causes error
C. Missing closing =end
D. Multi-line comment syntax is correct
Solution
Step 1: Check multi-line comment syntax rules
In Ruby, =begin and =end must be at the start of the line with no leading spaces.
Step 2: Analyze the given code
If =begin or =end are indented or not at line start, Ruby raises a syntax error.
Final Answer:
=begin and =end must be at the start of the line -> Option A
Quick Check:
Multi-line comment markers must start line [OK]
Hint: =begin and =end must start the line exactly [OK]
Common Mistakes:
Indenting =begin or =end
Thinking # inside multi-line comment causes error
Forgetting to close multi-line comment
5. You want to document a Ruby method that takes a number and returns its square. Which comment style best describes the method's purpose and input/output?
hard
A. =begin
Squares a number
Input: num
Output: squared number
=end
B. # This method squares a number
# @param num [Integer] the number to square
# @return [Integer] the squared result
C. // Squares a number
// Input: num
// Output: squared number
Ruby uses single-line comments with tags like @param and @return to document methods clearly.
Step 2: Compare options
# This method squares a number
# @param num [Integer] the number to square
# @return [Integer] the squared result uses Ruby documentation style. =begin
Squares a number
Input: num
Output: squared number
=end uses multi-line comment but lacks tags. Options C and D use invalid comment styles for Ruby.
Final Answer:
# This method squares a number
# @param num [Integer] the number to square
# @return [Integer] the squared result -> Option B
Quick Check:
Use # with @param and @return for Ruby docs [OK]
Hint: Use # with @param and @return to document Ruby methods [OK]
Common Mistakes:
Using multi-line comments without tags for documentation
Using comment styles from other languages
Not specifying input/output clearly
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.