Skip to content

Instantly share code, notes, and snippets.

@zomberg
Created August 23, 2017 15:33
Show Gist options
  • Save zomberg/8ce80f291f3d7952b482c8b88608d4fd to your computer and use it in GitHub Desktop.
Save zomberg/8ce80f291f3d7952b482c8b88608d4fd to your computer and use it in GitHub Desktop.
Flattify array on Ruby
def flattify(array)
array.each_with_object([]) do |element, result|
flattened = element.is_a?(Array) ? flattify(element) : element
result.push(flattened)
end
end
puts flattify([[1, 2, [3]], 4])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment