Created
June 23, 2018 16:46
-
-
Save pranavcode/4591d5fc2228f94597f1e13ef4cb46b5 to your computer and use it in GitHub Desktop.
Flatten the array in ruby without using inbuilt Array#flatten
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
#!/usr/bin/env ruby | |
def flatten(array) | |
array.each_with_object([]) do |e, flat| | |
flat.push *(e.is_a?(Array) ? flatten(e) : e) | |
end | |
end | |
print flatten([[1, 2, 3, 4], 5, 6, [[7, 8], 9, 0]]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment