Ruby - Basics and RuntimeGiven a nested array, how do you flatten it into a single array in Ruby?AUse array.compactBUse array.joinCUse array.flattenDUse array.reduceCheck Answer
Step-by-Step SolutionSolution:Step 1: Identify method to flatten arraysflatten returns a new array with all nested arrays merged into one.Step 2: Check other methodsjoin converts to string, compact removes nils, reduce combines elements but not flatten.Final Answer:Use array.flatten -> Option CQuick Check:Flatten nested arrays with flatten method [OK]Quick Trick: Use flatten to convert nested arrays to single array [OK]Common Mistakes:MISTAKESUsing join which returns stringUsing compact which removes nilsConfusing reduce with flatten
Master "Basics and Runtime" in Ruby9 interactive learning modes - each teaches the same concept differentlyLearnWhyDeepVisualTryChallengeProjectRecallTime
More Ruby Quizzes Arrays - Array comparison and set operations - Quiz 9hard Arrays - Array slicing and ranges - Quiz 8hard Arrays - Accessing elements (indexing, first, last) - Quiz 3easy Arrays - Array creation methods - Quiz 12easy Methods - Variable-length arguments (*args) - Quiz 13medium Operators and Expressions - Conditional assignment (||=) - Quiz 10hard Ruby Basics and Runtime - Everything is an object mental model - Quiz 3easy Ruby Basics and Runtime - How Ruby interprets code at runtime - Quiz 6medium Variables and Data Types - Local variables and naming conventions - Quiz 2easy Variables and Data Types - Type checking with .class and .is_a? - Quiz 7medium