Skip to content

Instantly share code, notes, and snippets.

@phantomfive
Created April 24, 2017 15:25
Show Gist options
  • Save phantomfive/1d551decac97b79a40423f3b223c3008 to your computer and use it in GitHub Desktop.
Save phantomfive/1d551decac97b79a40423f3b223c3008 to your computer and use it in GitHub Desktop.
#
# Converts an array with nested elements
# into a flattened array with the nested
# elements inline.
#
# == Parameters:
# arr::
# This parameter can be an array or
# an single object, or nil. If it
# is a single object or nil, it will
# be converted to an array for you.
#
# == Returns:
# A flattened copy of the array.
# If nil was passed, [nil] will
# be returned.
#
def flattenArray(arr)
rv = []
return rv.push(arr) if arr.class != Array
arr.each do |subArray|
flattenedSub = flattenArray(subArray)
rv.concat(flattenedSub)
end
rv
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment