Skip to content

Instantly share code, notes, and snippets.

@BrianPurgert
Last active December 5, 2018 03:11
Show Gist options
  • Save BrianPurgert/de3aa855e8405a26e026855c65620d88 to your computer and use it in GitHub Desktop.
Save BrianPurgert/de3aa855e8405a26e026855c65620d88 to your computer and use it in GitHub Desktop.
flatten an array of arbitrarily nested arrays of integers into a flat array of integers
# flattens arbitrarily nested arrays of integers into a flat array of integers.
#
# Examples:
#
# flatten_integer_arrays([[1, 2, 3], [4, 5, 6, [7, 8]], 9, 10])
# # [1,2,3,4,5,6,7,8,9,10]
#
def flatten_integer_arrays(nested)
nested.to_s.scan(/\d+/).map(&:to_i)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment