Created
April 3, 2018 06:51
-
-
Save quezacoatl/17dbee0f3b979b5362181c611a1a8578 to your computer and use it in GitHub Desktop.
Flatten array in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def flatten(arr, flat=[]) | |
arr.each_with_object(flat) do |item, result| | |
if item.is_a?(Array) | |
flatten(item, flat) | |
else | |
result << item | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment