Skip to content

Instantly share code, notes, and snippets.

@pranavcode
Created June 23, 2018 16:46
Show Gist options
  • Save pranavcode/4591d5fc2228f94597f1e13ef4cb46b5 to your computer and use it in GitHub Desktop.
Save pranavcode/4591d5fc2228f94597f1e13ef4cb46b5 to your computer and use it in GitHub Desktop.
Flatten the array in ruby without using inbuilt Array#flatten
#!/usr/bin/env ruby
def flatten(array)
array.each_with_object([]) do |e, flat|
flat.push *(e.is_a?(Array) ? flatten(e) : e)
end
end
print flatten([[1, 2, 3, 4], 5, 6, [[7, 8], 9, 0]])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment