Skip to content

Instantly share code, notes, and snippets.

@pedrolopez
Created September 22, 2017 22:04
Show Gist options
  • Save pedrolopez/aecf419f5b721692415ce9bab5fce4ed to your computer and use it in GitHub Desktop.
Save pedrolopez/aecf419f5b721692415ce9bab5fce4ed to your computer and use it in GitHub Desktop.
Flatten an array
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