Bird
0
0

In Ruby, what is the primary purpose of using the each method on an array?

easy📝 Conceptual Q1 of 15
Ruby - Enumerable and Collection Processing
In Ruby, what is the primary purpose of using the each method on an array?
ATo remove duplicate elements from the array
BTo sort the elements of the array in ascending order
CTo execute a block of code for every element in the array
DTo convert the array into a string
Step-by-Step Solution
Solution:
  1. Step 1: Understand the each method

    The each method is used to iterate over each element in a collection.
  2. Step 2: Identify its purpose

    It executes the given block of code once for every element in the array.
  3. Final Answer:

    To execute a block of code for every element in the array -> Option C
  4. Quick Check:

    each runs code on each element [OK]
Quick Trick: Each runs code on every element in a collection [OK]
Common Mistakes:
  • Confusing each with sorting or filtering methods
  • Thinking each modifies the array itself
  • Assuming each returns a new array

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes