Created
December 16, 2016 10:08
-
-
Save arman-h/6ab53211a66cc27f3348c0f38312d296 to your computer and use it in GitHub Desktop.
Flatten an array with Ruby without using built-in methods
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
| 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], 5]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment