Skip to content

Instantly share code, notes, and snippets.

@l3thal
Last active September 17, 2015 18:18
Show Gist options
  • Save l3thal/29aa6dd023d8f4498b32 to your computer and use it in GitHub Desktop.
Save l3thal/29aa6dd023d8f4498b32 to your computer and use it in GitHub Desktop.
flatten an array of arbitrarily nested integers
def flatten(list, result=[])
list.each do |item|
if item.is_a? Integer
result << item
else
result += flatten(item)
end
end
res
end
flatten [1, 2, [3, 4], [[5, 6], [7], 8]]
=> [1, 2, 3, 4, 5, 6, 7, 8]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment