Skip to content

Instantly share code, notes, and snippets.

@AAlvarez90
Created September 19, 2016 16:37
Show Gist options
  • Save AAlvarez90/9dccdc2f66cb7883c6c54516c2c572d4 to your computer and use it in GitHub Desktop.
Save AAlvarez90/9dccdc2f66cb7883c6c54516c2c572d4 to your computer and use it in GitHub Desktop.
A method to flatten an array of integers
def array_flatten(array, result = [])
array.each do |element|
if element.is_a? Integer
result.push element
else
array_flatten(element, result)
end
end
result
end
result = array_flatten([[1,11,[20,21]],2,3,[4,5,6],7,[9,10,[45,78]]])
puts result.inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment