如何采用子阵列,并将其展平为父阵列?

时间:2009-11-09 23:16:27

标签: ruby

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

问题是,一旦发现一个数组,我就会将它展平,然后需要将它插入到原始父数组中,这样A,B,C,a,b,c,d,D就会被循环一次。

1 个答案:

答案 0 :(得分:1)

从描述中可以看出你能够做到

parent.flatten.each do |child|
end