Ruby: How to concatenate array of arrays into one
You're looking for #flatten
:
concatenated = array_of_arrays.flatten
By default, this will flatten the lists recursively. #flatten
accepts an optional argument to limit the recursion depth – the documentation lists examples to illustrate the difference.
Or more generally:
array_of_arrays.reduce(:concat)
You can use flatten! method. eg.
a = [ 1, 2, [3, [4, 5] ] ]
a.flatten! #=> [1, 2, 3, 4, 5]