From: 7stud -- Date: 2009-09-10T13:44:30+09:00 Subject: Re: How Are Variables Kept Independent of Each Other Yet Pas Yossef Mendelssohn wrote: > > Mason, you're going to want to make an actual copy of the original > variable, not simply a new pointer to it (which is what you get when > you do something like `b = a`). In many cases, calling .dup or .clone > will work. (As in `b = a.dup` or `b = a.clone`.) > > However, since you have an array of arrays, you're going to need a > "deep copy". I believe `b = Marshal.load(Marshal.dump(a))` is the > standard idiom. Or, if that offends your sensibilities, in your case you can do this: arr = [ [1, 2], ["a", "b"] ] arr_copy = [] arr.each do |subarr| arr_copy << subarr.dup end p arr p arr_copy --output:-- [[1, 2], ["a", "b"]] [[1, 2], ["a", "b"]] arr[0][1] = "******" p arr p arr_copy --output:-- [[1, "******"], ["a", "b"]] [[1, 2], ["a", "b"]] -- Posted via http://www.ruby-forum.com/.