Last active
September 23, 2020 17:07
-
-
Save ceolinrenato/221f582d8b1a554d5895bb5d4889d404 to your computer and use it in GitHub Desktop.
Recursive flatten array
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
# frozen_string_literal: true | |
def flatten_array(array) | |
raise ArgumentError unless array.class == Array | |
flattened = [] | |
array.each do |item| | |
if item.class == Array | |
flattened += flatten_array(item) | |
else | |
flattened << item | |
end | |
end | |
flattened | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment