Skip to content

Instantly share code, notes, and snippets.

@neerajkumar
Last active December 15, 2017 05:53
Show Gist options
  • Save neerajkumar/84844b96911d8a611be2bbdb20c30ae7 to your computer and use it in GitHub Desktop.
Save neerajkumar/84844b96911d8a611be2bbdb20c30ae7 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