Skip to content

Instantly share code, notes, and snippets.

@bayendor
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bayendor/724fbeead5a990244c93 to your computer and use it in GitHub Desktop.
Save bayendor/724fbeead5a990244c93 to your computer and use it in GitHub Desktop.
Flatten an array in ruby without using `flatten`
def flatten_clone(array, level = -1, result = [])
array.each do |element|
if element.is_a?(Array) && level != 0
flatten_clone element, level - 1, result
else
result << element
end
end
result
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment