Bird
0
0

Which of the following Ruby code snippets correctly uses an iterator to print each element of the array arr?

easy📝 Syntax Q3 of 15
Ruby - Loops and Iteration
Which of the following Ruby code snippets correctly uses an iterator to print each element of the array arr?
Aarr.loop { |element| puts element }
Bfor element in arr puts element end
Carr.each { |element| puts element }
Darr.each do element puts element end
Step-by-Step Solution
Solution:
  1. Step 1: Review Ruby iterator syntax

    The correct syntax for each uses a block with a pipe-delimited parameter.
  2. Step 2: Analyze options

    arr.each { |element| puts element } correctly uses arr.each { |element| puts element }. for element in arr puts element end uses a loop, not an iterator. arr.loop { |element| puts element } uses a non-existent method loop on array. arr.each do element puts element end misses the pipe delimiters in the block.
  3. Final Answer:

    arr.each { |element| puts element } -> Option C
  4. Quick Check:

    Correct iterator syntax requires pipes around block parameters [OK]
Quick Trick: Use pipes | | around block variables in iterators [OK]
Common Mistakes:
  • Omitting pipes around block parameters
  • Confusing loops with iterators
  • Using non-existent iterator methods

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes