Jan Dvorak
12/25/2007 2:34:00 PM
On Monday 24 December 2007 16:19:59 jbieger@gmail.com wrote:
> Hi, I'm a total newbie with Ruby and I was trying to make this
> function that would get a (multidimensional) array, make a copy, make
> some changes to the copy and then return the copy, without altering
> the original array. However, no matter what I try, the original array
> gets altered. Here is my code:
Another possibility is serialization - it's still your job to make your
classes serializable and there is stuff that cannot be serialized, but for
simple cases (multi-dimensional arrays) it works out-of-box:
irb(main):001:0> a = [["a","b"],[1,2]]
=> [["a", "b"], [1, 2]]
irb(main):002:0> b = Marshal.load(Marshal.dump(a))
=> [["a", "b"], [1, 2]]
irb(main):003:0> b[0][0], b[1][0] = "x", 9
=> ["x", 9]
irb(main):004:0> a
=> [["a", "b"], [1, 2]]
irb(main):005:0> b
=> [["x", "b"], [9, 2]]
Jan