Bird
0
0

What does the each method do in Ruby when used on an array?

easy📝 Conceptual Q11 of 15
Ruby - Loops and Iteration
What does the each method do in Ruby when used on an array?
AIt returns a new array with modified items.
BIt runs a block of code once for every item in the array.
CIt changes the array by removing items.
DIt sorts the array in ascending order.
Step-by-Step Solution
Solution:
  1. Step 1: Understand the purpose of each

    The each method is used to run a block of code for every item in a collection like an array.
  2. Step 2: Check what each does to the array

    It does not change or sort the array; it simply processes each item one by one.
  3. Final Answer:

    It runs a block of code once for every item in the array. -> Option B
  4. Quick Check:

    each runs code on each item [OK]
Quick Trick: Remember: each processes items, does not change array [OK]
Common Mistakes:
MISTAKES
  • Thinking each modifies the array
  • Confusing each with methods like map
  • Assuming each returns a new array

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes