Bird
0
0

What is wrong with this Ruby code snippet?

medium📝 Debug Q14 of 15
Ruby - Loops and Iteration
What is wrong with this Ruby code snippet?
items = [1, 2, 3]
items.each do item
  puts item * 2
end
AArray <code>items</code> is empty
BUsing <code>do</code> instead of braces is invalid
CCannot multiply numbers inside <code>each</code>
DMissing pipes around block variable: should be <code>|item|</code>
Step-by-Step Solution
Solution:
  1. Step 1: Check block variable syntax in each do block

    When using do with each, the block variable must be inside pipes: do |item|.
  2. Step 2: Identify the error

    The code misses pipes around item, causing a syntax error.
  3. Final Answer:

    Missing pipes around block variable: should be |item| -> Option D
  4. Quick Check:

    Block variables need pipes | | [OK]
Quick Trick: Always put block variables inside | | with each do [OK]
Common Mistakes:
MISTAKES
  • Omitting pipes around block variables
  • Thinking do is invalid with each
  • Assuming multiplication inside each is wrong

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes