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?
arr = [1, 2, 3]
arr.each do n
  puts n
end
AThe block parameter should be enclosed in pipes: <code>do |n|</code>
BThe array <code>arr</code> is not defined properly
CThe <code>each</code> method cannot be used with <code>do</code> blocks
DThe <code>puts</code> method cannot be called inside an iterator
Step-by-Step Solution
Solution:
  1. Step 1: Understand block syntax in Ruby

    When using do...end blocks with iterators, block parameters must be enclosed in pipes.
  2. Step 2: Identify the error

    The code uses do n instead of do |n|, which is a syntax error.
  3. Final Answer:

    The block parameter should be enclosed in pipes: do |n| -> Option A
  4. Quick Check:

    Block parameters always require pipes [OK]
Quick Trick: Always use pipes | | for block parameters in iterators [OK]
Common Mistakes:
  • Forgetting pipes around block variables
  • Misusing do without block parameters
  • Assuming each can't use do...end

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes