So I have a basic problem that needs to be solved. I need to take the following nested array [1, 2, ["str", 1], [[2]]] and return [1,2,"str",1,2]. Ultimately removing all nested arrays. The following is my incomplete solution:
test = [1, 2, ["str", 1], [[2]]]
def remove_array(array)
new_array = []
array.each do |item|
if item != Array
puts "you are here"
new_array << item
else
remove_array(item)
end
end
end
remove_array(test)
This is a classic recursion method. However, I cant seem to think of how to maintain new_array
. Every time I pass the item that is an array to it, new_array
doesn't maintain its state. I would also like to point out that I am aware of the built in method flatten
as I am trying to solve the solution without said method. Any advice would be greatly appreciated.