Created
November 16, 2016 17:36
-
-
Save usmanasif/4a9e1a295faba0d2ef597158d11856d2 to your computer and use it in GitHub Desktop.
Flatten method
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) | |
puts array.inspect | |
result = [] | |
array.each_with_object([]) do |a, result| | |
result.push *((a.is_a?(Array) ? flatten(a) : a)) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment