Skip to content

Instantly share code, notes, and snippets.

@arman-h
Created December 16, 2016 10:08
Show Gist options
  • Select an option

  • Save arman-h/6ab53211a66cc27f3348c0f38312d296 to your computer and use it in GitHub Desktop.

Select an option

Save arman-h/6ab53211a66cc27f3348c0f38312d296 to your computer and use it in GitHub Desktop.
Flatten an array with Ruby without using built-in methods
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