Bird
0
0

Given a nested array, how do you flatten it into a single array in Ruby?

hard📝 Application Q9 of 15
Ruby - Basics and Runtime
Given a nested array, how do you flatten it into a single array in Ruby?
AUse array.compact
BUse array.join
CUse array.flatten
DUse array.reduce
Step-by-Step Solution
Solution:
  1. Step 1: Identify method to flatten arrays

    flatten returns a new array with all nested arrays merged into one.
  2. Step 2: Check other methods

    join converts to string, compact removes nils, reduce combines elements but not flatten.
  3. Final Answer:

    Use array.flatten -> Option C
  4. Quick Check:

    Flatten nested arrays with flatten method [OK]
Quick Trick: Use flatten to convert nested arrays to single array [OK]
Common Mistakes:
MISTAKES
  • Using join which returns string
  • Using compact which removes nils
  • Confusing reduce with flatten

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Ruby Quizzes