Created
June 23, 2017 04:22
-
-
Save hernamvel/a8230a6ab55ef886b793b4b37a2a7c8c to your computer and use it in GitHub Desktop.
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(integer_array) | |
result = [] | |
integer_array.each do |i| | |
if i.is_a? Array | |
# It's and array. Go a recursevely flatten this child element | |
child_array = flatten i | |
result.concat child_array | |
else | |
# It's an integer. Just added to the final array | |
result << i | |
end | |
end | |
result | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment