Bird
0
0

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

easy📝 Conceptual Q11 of 15
Ruby - Enumerable and Collection Processing
What does the Ruby method inject do when used on an array?
AIt combines all elements into a single value using a block.
BIt sorts the array in ascending order.
CIt removes duplicate elements from the array.
DIt returns the first element of the array.
Step-by-Step Solution
Solution:
  1. Step 1: Understand the purpose of inject

    The inject method takes all elements of an array and combines them into one value using the block provided.
  2. Step 2: Compare with other options

    Sorting, removing duplicates, or returning the first element are different methods like sort, uniq, or first.
  3. Final Answer:

    It combines all elements into a single value using a block. -> Option A
  4. Quick Check:

    inject combines elements = C [OK]
Quick Trick: Remember: inject reduces many to one value [OK]
Common Mistakes:
  • Confusing inject with sorting methods
  • Thinking inject returns an array
  • Assuming inject removes duplicates

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes