Ruby: Flatten arrays by only one level

Posted About 9 years ago. Visible to the public.

Array#flatten Show archive.org snapshot by default flattens an array recursively. To only flatten the array for e.g. one level, it takes an optional argument.

# Flattens the array recursively
>> [1, [2, [3]]].flatten
=> [1, 2, 3]

# Flattens the array the given number of times
>> [1, [2, [3]]].flatten(1)
=> [1, 2, [3]]
Thomas Klemm
Last edit
About 9 years ago
Thomas Klemm
License
Source code in this card is licensed under the MIT License.
Posted by Thomas Klemm to makandra dev (2015-02-20 13:51)