Created
April 24, 2017 15:25
-
-
Save phantomfive/1d551decac97b79a40423f3b223c3008 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# 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