Created
May 13, 2019 15:24
-
-
Save aashish/e70c18af404f4be5537e187671580307 to your computer and use it in GitHub Desktop.
Array of arrays will be flattened.
This file contains hidden or 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
class Array | |
def flattener | |
arr = [] | |
self.each do |x| | |
if x.is_a? Array | |
arr = arr + x.flattener | |
else | |
arr << x | |
end | |
end | |
return arr | |
end | |
end | |
p [3, [4, [5, 6]], 7].flattener | |
#ouput [3, 4, 5, 6, 7] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment