Skip to content

Instantly share code, notes, and snippets.

@aziflaj
Created January 10, 2018 08:56
Show Gist options
  • Save aziflaj/be34e0a0665346b6ea7f013dfffdea64 to your computer and use it in GitHub Desktop.
Save aziflaj/be34e0a0665346b6ea7f013dfffdea64 to your computer and use it in GitHub Desktop.
def flatten(arr, level = 1)
r = arr.each_with_object([]) do |item, result|
if item.is_a?(Array)
item.each { |i| result << i }
else
result << item
end
end
return flatten(r, level - 1) if level.positive?
r
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment