Skip to content

Instantly share code, notes, and snippets.

@rtacconi
Created August 7, 2016 18:36
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 rtacconi/d5a6f62776bf275c056985915e0c15d7 to your computer and use it in GitHub Desktop.
Save rtacconi/d5a6f62776bf275c056985915e0c15d7 to your computer and use it in GitHub Desktop.
Flatten an array without using the Ruby library
def flatten(in_array, out_array = [])
in_array.each do |a|
if a.class == Array
flatten(a, out_array)
else
out_array << a
end
end
out_array
end
p flatten([[1, 3], [5, [7, 9]]]) # => [1, 3, 5, 7, 9]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment