Created
May 19, 2016 16:00
-
-
Save mathamoz/bb0f240edbc51f1b79c9b5e5591d32ea to your computer and use it in GitHub Desktop.
Simple Ruby function to recursively flatten an array and return the result
This file contains 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
def flatten_array (arr, result=[]) | |
arr.each do |el| | |
if el.is_a?(Array) | |
flatten_array(el, result) | |
else | |
result.push(el) | |
end | |
end | |
result | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment