Ruby: Flatten arrays by only one level

Posted . 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]]
Last edit
Deleted user #3651
License
Source code in this card is licensed under the MIT License.
Posted to makandra dev (2015-02-20 13:51)