Created
January 20, 2019 18:54
-
-
Save vikas95prasad/34ffc82fb0af77f1b74abb37448253c5 to your computer and use it in GitHub Desktop.
Ruby flatten method with recursive function
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(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