Bird
0
0

What does the Ruby method compact do when called on an array?

easy📝 Conceptual Q11 of 15
Ruby - Arrays

What does the Ruby method compact do when called on an array?

AIt returns a new array without any <code>nil</code> values.
BIt removes all duplicate values from the array.
CIt sorts the array in ascending order.
DIt changes all <code>nil</code> values to zero.
Step-by-Step Solution
Solution:
  1. Step 1: Understand the purpose of compact

    The compact method creates a new array by removing all nil values from the original array.
  2. Step 2: Compare with other options

    Other options describe different array methods or behaviors, but only compact removes nil values without modifying the original array.
  3. Final Answer:

    It returns a new array without any nil values. -> Option A
  4. Quick Check:

    compact removes nil values = B [OK]
Quick Trick: Remember: compact removes nil, keeps original unchanged [OK]
Common Mistakes:
MISTAKES
  • Confusing compact with uniq (removes duplicates)
  • Thinking compact sorts the array
  • Believing compact replaces nil with zero

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes