0
parent = ["A","B","C",["a","b", "c", "d"], "D"]

parent.each do |children|

if children.is_a?

children.flatten # how do i insert it back to parent
# so that this loop can continue looping through the remainder
#including the newly flattened children(a,b,c,d)

end

end

The question is, once an array is discovered, i flatten it, and need it to be inserted it to the original parent array, so that A,B,C,a,b,c,d,D will be looped once.

1 Answer 1

1

From the description it seems that you would be able to do

parent.flatten.each do |child|
end
Sign up to request clarification or add additional context in comments.

2 Comments

When you say "need it to be inserted it to the original parent array" you mean you want the flattened contents to overwrite the original contents, modify the iterator to read: parent.flatten!.each do |child| ... end
yes that makes sense. as flatten! overwrites itself. flatten produces new array. thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.