Bird
Raised Fist0
Rubyprogramming~20 mins

Comments and documentation in Ruby - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Ruby Comments Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of code with inline and block comments
What is the output of this Ruby code that uses both inline and block comments?
Ruby
puts 5 + 3 # adds five and three
=begin
This is a block comment
It should be ignored by Ruby
=end
puts 2 * 4
A8\n2 * 4
BSyntaxError
C5 + 3\n2 * 4
D8\n8
Attempts:
2 left
💡 Hint
Remember that comments do not affect the output and are ignored by Ruby.
❓ Predict Output
intermediate
2:00remaining
Effect of comments on variable assignment
What will be the value of variable x after running this Ruby code?
Ruby
x = 10 # initial value
# x = 20
x = 30 # updated value
A30
Bnil
C20
D10
Attempts:
2 left
💡 Hint
Lines starting with # are ignored by Ruby.
🔧 Debug
advanced
2:00remaining
Identify the syntax error caused by incorrect comment usage
Which option shows code that will cause a syntax error due to incorrect comment usage in Ruby?
A# This is a valid comment\nputs 'World'
B=begin\nThis is a block comment without an end
Cputs 'Hello' # This is a comment
Dputs 'Test' # Another comment
Attempts:
2 left
💡 Hint
Block comments must have both =begin and =end.
🧠 Conceptual
advanced
1:30remaining
Purpose of documentation comments in Ruby
What is the main purpose of documentation comments in Ruby code?
ATo add extra functionality to the program
BTo make the code run faster
CTo explain the code to humans without affecting execution
DTo cause errors if the code is incorrect
Attempts:
2 left
💡 Hint
Think about why programmers write comments.
❓ Predict Output
expert
2:30remaining
Output of code with nested comments and string literals
What is the output of this Ruby code snippet?
Ruby
puts "Hello # not a comment"
# puts "This line is commented out"
=begin
puts "This is inside a block comment"
=end
puts 'Goodbye'
AHello # not a comment\nGoodbye
BHello\nGoodbye
CHello # not a comment\nThis line is commented out\nGoodbye
DSyntaxError
Attempts:
2 left
💡 Hint
Comments do not affect strings inside quotes.

Practice

(1/5)
1. What symbol is used to start a single-line comment in Ruby?
easy
A. #
B. //
C.
D. /*

Solution

  1. Step 1: Identify single-line comment syntax in Ruby

    Ruby uses # to start a single-line comment.
  2. Step 2: Compare with other options

    Options A, B, and C are comment symbols from other languages (JavaScript, HTML, C-style).
  3. Final Answer:

    # -> Option A
  4. 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

  1. Step 1: Recall Ruby multi-line comment syntax

    Ruby uses =begin and =end to mark multi-line comments.
  2. Step 2: Check other options

    Options B, C, and D are multi-line comment styles from other languages (C, HTML, JavaScript).
  3. Final Answer:

    =begin This is a multi-line comment =end -> Option D
  4. 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

  1. 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.
  2. Step 2: Check the output of puts greet("Alice")

    It prints the string "Hello, Alice!" to the console.
  3. Final Answer:

    Hello, Alice! -> Option C
  4. Quick Check:

    Comment ignored, method returns greeting [OK]
Hint: Comments don't print; method returns greeting string [OK]
Common Mistakes:
  • Thinking comments print output
  • Confusing method name with output
  • Expecting comment text to appear
4. Identify the error in this Ruby code snippet:
  =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

  1. 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.
  2. Step 2: Analyze the given code

    If =begin or =end are indented or not at line start, Ruby raises a syntax error.
  3. Final Answer:

    =begin and =end must be at the start of the line -> Option A
  4. 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
D.

Solution

  1. Step 1: Identify proper Ruby documentation comment style

    Ruby uses single-line comments with tags like @param and @return to document methods clearly.
  2. 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.
  3. Final Answer:

    # This method squares a number # @param num [Integer] the number to square # @return [Integer] the squared result -> Option B
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard