Skip to content

Instantly share code, notes, and snippets.

@Sayanc93
Created December 14, 2016 20:46
Show Gist options
  • Save Sayanc93/4f063986ca998f475538fd154018fc42 to your computer and use it in GitHub Desktop.
Save Sayanc93/4f063986ca998f475538fd154018fc42 to your computer and use it in GitHub Desktop.
Ruby Array#flatten implementation
def flatten(array)
# Input: Array or Array of Arrays
# Output: A flattened Array
flat_arr = []
array.each do |value|
if value.is_a?(Array)
flat_arr.concat(flatten(value))
else
flat_arr << value
end
end
return flat_arr
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment