Created
August 23, 2017 15:33
-
-
Save zomberg/8ce80f291f3d7952b482c8b88608d4fd to your computer and use it in GitHub Desktop.
Flattify array on Ruby
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
def flattify(array) | |
array.each_with_object([]) do |element, result| | |
flattened = element.is_a?(Array) ? flattify(element) : element | |
result.push(flattened) | |
end | |
end | |
puts flattify([[1, 2, [3]], 4]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment