Created
May 10, 2019 19:26
-
-
Save aaronchi/62ca0f91a7678b692359f9729fca8dbe to your computer and use it in GitHub Desktop.
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
# Extends Array class with a new flatten method | |
# Type checking is not done since this is extending Array | |
# Uses simple recursion | |
class Array | |
def re_flatten | |
flat = [] | |
self.each do |a| | |
if a.is_a?(Array) | |
flat += a.re_flatten | |
else | |
flat << a | |
end | |
end | |
flat | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment