Skip to content

Instantly share code, notes, and snippets.

@joelibaceta
Created July 9, 2019 22:34
Show Gist options
  • Save joelibaceta/08013565c41009d5f71ed917544bd317 to your computer and use it in GitHub Desktop.
Save joelibaceta/08013565c41009d5f71ed917544bd317 to your computer and use it in GitHub Desktop.
Flatten Array
def flatten(input)
if input.any? { |n| n.is_a? Array }
return input.reduce([]) do |b, x|
x.is_a?(Array) ? b + flatten(x) : b + [x]
end
else
return input
end
end
# p flatten([4,5, [6, 7, [8, 9]]])
# [4, 5, 6, 7, 8, 9]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment