Is there an easy way to copy a nested Array so that every object in the array will be a 'dup' of the original? I recently run into this:
irb(main):001:0> a = [[1,2],[3,4]]
=> [[1, 2], [3, 4]]
irb(main):002:0> b = a.dup
=> [[1, 2], [3, 4]]
irb(main):003:0> a[0][1] = 99
=> 99
irb(main):004:0> a
=> [[1, 99], [3, 4]]
irb(main):005:0> b
=> [[1, 99], [3, 4]]
irb(main):006:0> a[0] = [101,102]
=> [101, 102]
irb(main):007:0> a
=> [[101, 102], [3, 4]]
irb(main):008:0> b
=> [[1, 99], [3, 4]]
So while the first level of arrays in a
were individual objects, their content were not, a[0][1]
is still equal to b[0][1]
. A general solution don't even have to know how deeply an array is nested. Walking through every object and make it a dup of itself sounds a bit brute-force to me.