Skip to content

Instantly share code, notes, and snippets.

@kitwalker12
Created January 10, 2018 00:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kitwalker12/62f09d79f44dd19e2f2e7823f105b79f to your computer and use it in GitHub Desktop.
Save kitwalker12/62f09d79f44dd19e2f2e7823f105b79f to your computer and use it in GitHub Desktop.
flatten an array of arbitrarily nested arrays
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