Created
December 14, 2016 20:46
-
-
Save Sayanc93/4f063986ca998f475538fd154018fc42 to your computer and use it in GitHub Desktop.
Ruby Array#flatten implementation
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 flatten(array) | |
| # Input: Array or Array of Arrays | |
| # Output: A flattened Array | |
| flat_arr = [] | |
| array.each do |value| | |
| if value.is_a?(Array) | |
| flat_arr.concat(flatten(value)) | |
| else | |
| flat_arr << value | |
| end | |
| end | |
| return flat_arr | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment