Skip to content

Instantly share code, notes, and snippets.

@ceolinrenato
Last active September 23, 2020 17:07
Show Gist options
  • Save ceolinrenato/221f582d8b1a554d5895bb5d4889d404 to your computer and use it in GitHub Desktop.
Save ceolinrenato/221f582d8b1a554d5895bb5d4889d404 to your computer and use it in GitHub Desktop.
Recursive flatten array
# 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