Bird
0
0

What is the issue with this Ruby code snippet?

medium📝 Debug Q6 of 15
Ruby - Loops and Iteration
What is the issue with this Ruby code snippet?
values = [4, 5, 6]
values.each |v| do
  puts v
end
AThe variable 'v' should be declared outside the block.
BThe array 'values' is not defined properly.
CThe 'puts' method cannot be used inside an each block.
DThe block syntax is incorrect; 'do' should come before the block parameter.
Step-by-Step Solution
Solution:
  1. Step 1: Identify block syntax

    The block parameters must be enclosed in pipes (| |) immediately after 'do' or '{'.
  2. Step 2: Correct the syntax

    Change values.each |v| do to values.each do |v|.
  3. Final Answer:

    The block syntax is incorrect; 'do' should come before the block parameter. -> Option D
  4. Quick Check:

    Block parameters must follow 'do' or '{' [OK]
Quick Trick: Block parameters go after 'do' or '{' [OK]
Common Mistakes:
MISTAKES
  • Placing block parameters before 'do'
  • Using incorrect block delimiters
  • Misplacing pipes around block variables

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes