Bird
0
0

Which of the following is the correct syntax for iterating over an array arr in Ruby?

easy📝 Syntax Q12 of 15
Ruby - Loops and Iteration
Which of the following is the correct syntax for iterating over an array arr in Ruby?
Afor i in arr puts i end
Barr.each { |item| puts item }
Carr.loop { |item| puts item }
Darr.iterate do |i| puts i end
Step-by-Step Solution
Solution:
  1. Step 1: Identify valid Ruby iterator syntax

    The method each is the standard iterator for collections in Ruby.
  2. Step 2: Check each option

    arr.each { |item| puts item } uses arr.each { |item| puts item }, which is correct syntax. The for loop "for i in arr puts i end" has invalid syntax (missing 'do'), options C and D use invalid method names.
  3. Final Answer:

    arr.each { |item| puts item } -> Option B
  4. Quick Check:

    Correct iterator syntax = A [OK]
Quick Trick: Look for each with block syntax for iteration [OK]
Common Mistakes:
MISTAKES
  • Using non-existent methods like loop or iterate
  • Confusing loops with iterators
  • Missing block syntax with pipes | |

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes