Created
January 10, 2018 00:07
-
-
Save kitwalker12/62f09d79f44dd19e2f2e7823f105b79f to your computer and use it in GitHub Desktop.
flatten an array of arbitrarily nested arrays
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 flatten_array(arr) | |
arr.reduce([]) do |res, item| | |
item.kind_of?(Array) ? res + flatten_array(item) : res << item | |
end | |
end | |
# flatten_array( [[1,2,[3]],4]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment