Skip to content

Instantly share code, notes, and snippets.

@bernardobarreto
Last active June 27, 2019 09:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bernardobarreto/4d62adf9aa21d7558d8ca3dc73e77f44 to your computer and use it in GitHub Desktop.
Save bernardobarreto/4d62adf9aa21d7558d8ca3dc73e77f44 to your computer and use it in GitHub Desktop.
ruby array flatten example
def flatten(array)
result = []
array.each do |element|
if element.class == Array
flatten(element).each do |sub|
result << sub
end
else
result << element
end
end
result
end
puts flatten([[1,2,[3]],4]) #=> [1,2,3,4]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment