From: Mason Kelsey Date: 2009-09-10T23:19:12+09:00 Subject: Re: How Are Variables Kept Independent of Each Other Yet Pas --001517576466fe7955047339e131 Content-Type: text/plain; charset=ISO-8859-1 Much Thanks, 7stud. I'm puzzled by the Marshall plan you first had. Is Marshall a class that you have to access? The second method seems the better of the two methods, ignoring my delicate sensibilities or not. *Attention to Ruby instructors:* I would recommend that Ruby instructors make a bid deal about passing values from one variable to another. If a student is not made aware of this curiosity to Ruby, they will be very puzzled at how their code works. Transferance of value is a very common function in any language. Yet the several texts I've see so far, it is only mentioned in passing in one. Big mistake. In all the languages I used in the past this way of doing things is unique. Why Matz decided on this approach is worth mentioning. What problem was he solving by doing things this way? Again, thanks, No Sam On Thu, Sep 10, 2009 at 12:44 AM, 7stud -- wrote: > 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/. > > --001517576466fe7955047339e131--