Bird
0
0

What does the Ruby method map return when called on an array?

easy📝 Conceptual Q1 of 15
Ruby - Enumerable and Collection Processing
What does the Ruby method map return when called on an array?
AA hash with keys as indices and values as elements
BThe original array modified in place
CThe number of elements in the array
DA new array with the results of running the block on each element
Step-by-Step Solution
Solution:
  1. Step 1: Understand map

    The map method applies the given block to each element of the array.
  2. Step 2: Return value

    It returns a new array containing the results of the block for each element, without modifying the original array.
  3. Final Answer:

    A new array with the results of running the block on each element -> Option D
  4. Quick Check:

    [1,2,3].map { |n| n*2 } returns [2,4,6] [OK]
Quick Trick: map returns a new transformed array [OK]
Common Mistakes:
  • Thinking map modifies the original array
  • Confusing map with each
  • Expecting map to return a count

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes