Skip to content

Instantly share code, notes, and snippets.

@topher6345
Created August 24, 2015 19:24
Show Gist options
  • Save topher6345/eb78b4abde43f2ae62ff to your computer and use it in GitHub Desktop.
Save topher6345/eb78b4abde43f2ae62ff to your computer and use it in GitHub Desktop.
Monkey patch Array#flatten
class Array
def flatten
@result = []
_flatten(self)
end
private
def _flatten(collection)
collection.each do |item|
if item.is_a? Array
_flatten(item)
else
@result << item
next
end
end
@result
end
end
p [1,2,3, [4,5], [[[6,7]]]].flatten
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment