Skip to content

Instantly share code, notes, and snippets.

@pulkit21
Created August 31, 2017 17:02
Show Gist options
  • Save pulkit21/d10b6a7f63530f81865363f2bae944d5 to your computer and use it in GitHub Desktop.
Save pulkit21/d10b6a7f63530f81865363f2bae944d5 to your computer and use it in GitHub Desktop.
Write some code, that will flatten an array of arbitrarily nested arrays of integers into a flat array of integers. e.g. [[1,2,[3]],4] -> [1,2,3,4]
def flatten_array(array)
array.each_with_object([]) do |element, flat|
flat.push *(element.is_a?(Array) ? flatten_array(element) : element)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment