Created
July 9, 2019 22:34
-
-
Save joelibaceta/08013565c41009d5f71ed917544bd317 to your computer and use it in GitHub Desktop.
Flatten Array
This file contains hidden or 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(input) | |
if input.any? { |n| n.is_a? Array } | |
return input.reduce([]) do |b, x| | |
x.is_a?(Array) ? b + flatten(x) : b + [x] | |
end | |
else | |
return input | |
end | |
end | |
# p flatten([4,5, [6, 7, [8, 9]]]) | |
# [4, 5, 6, 7, 8, 9] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment