Bird
0
0

Identify the mistake in this Ruby code snippet:

medium📝 Debug Q6 of 15
Ruby - Loops and Iteration
Identify the mistake in this Ruby code snippet:
4.times { |i|
  puts i
AThe 'times' method cannot be used with a block
BThe block variable 'i' should be declared outside the block
CThe block is missing a closing brace '}'
DThe 'puts' method cannot be used inside a block
Step-by-Step Solution
Solution:
  1. Step 1: Check block syntax

    The code uses a block with curly braces but does not close it properly.
  2. Step 2: Identify missing brace

    The opening brace '{' is present but the closing brace '}' is missing.
  3. Final Answer:

    The block is missing a closing brace '}' -> Option C
  4. Quick Check:

    Proper block syntax requires matching braces [OK]
Quick Trick: Always close blocks with matching braces or keywords [OK]
Common Mistakes:
  • Forgetting to close a block with '}' or 'end'
  • Misplacing block variables outside the block
  • Assuming 'times' cannot take a block

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes