Ruby : Deep Copy

# problem
a = [[0, 1], [10], 19]
b = a.dup
b[0][1] = 100 #=> b = [[0, 100], [10], 19] and a = [[0, 100], [10], 19]

# solution
a = [[0, 1], [10], 19]
b = Marshal.load(Marshal.dump(a))
b[0][1] = 100 #=> b = [[0, 100], [10], 19] and a = [[0, 1], [10], 19]
One Response
  1. February 20, 2009