Created
September 22, 2017 22:04
-
-
Save pedrolopez/aecf419f5b721692415ce9bab5fce4ed to your computer and use it in GitHub Desktop.
Flatten an array
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 recursively_flatten_an_array(nested_array, flattened_array = []) | |
nested_array.each do |obj| | |
unless obj.is_a? Array | |
flattened_array << obj | |
else | |
recursively_flatten_an_array(obj, flattened_array) | |
end | |
end | |
return flattened_array | |
end | |
puts recursively_flatten_an_array([[1,2,[3]],4]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment