Created
August 24, 2015 19:24
-
-
Save topher6345/eb78b4abde43f2ae62ff to your computer and use it in GitHub Desktop.
Monkey patch Array#flatten
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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