Skip to content

Instantly share code, notes, and snippets.

@vikas95prasad
Created January 20, 2019 18:54
Show Gist options
  • Save vikas95prasad/34ffc82fb0af77f1b74abb37448253c5 to your computer and use it in GitHub Desktop.
Save vikas95prasad/34ffc82fb0af77f1b74abb37448253c5 to your computer and use it in GitHub Desktop.
Ruby flatten method with recursive function
def flatten(array)
flatten_arr = []
array.each do |element|
if element.kind_of? Array
flatten_arr += flatten(element)
else
flatten_arr << element
end
end
flatten_arr
end
flatten([1,2,3,[4,5,[6,7],[8]]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment