Skip to content

Instantly share code, notes, and snippets.

@hernamvel
Created June 23, 2017 04:22
Show Gist options
  • Save hernamvel/a8230a6ab55ef886b793b4b37a2a7c8c to your computer and use it in GitHub Desktop.
Save hernamvel/a8230a6ab55ef886b793b4b37a2a7c8c to your computer and use it in GitHub Desktop.
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