Bird
0
0

Identify the error in this Ruby code using an iterator:

medium📝 Debug Q14 of 15
Ruby - Loops and Iteration
Identify the error in this Ruby code using an iterator:
items = ["a", "b", "c"]
items.each do item
  puts item
end
AIncorrect method name, should be for_each
BNo error, code runs fine
CArray items cannot be iterated
DMissing block parameter pipes | | around item
Step-by-Step Solution
Solution:
  1. Step 1: Check block parameter syntax

    In Ruby, block parameters must be enclosed in pipes, like |item|.
  2. Step 2: Identify the missing pipes

    The code uses do item instead of do |item|, causing a syntax error.
  3. Final Answer:

    Missing block parameter pipes | | around item -> Option D
  4. Quick Check:

    Block parameters need | | = B [OK]
Quick Trick: Always use |param| in blocks for iterators [OK]
Common Mistakes:
  • Omitting pipes around block variables
  • Using wrong method names
  • Assuming arrays can't be iterated

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes