From: Mason Kelsey Date: 2009-09-10T23:58:03+09:00 Subject: Re: How Are Variables Kept Independent of Each Other Yet Pass Values? --00235447101495bd2c04733a6a60 Content-Type: text/plain; charset=ISO-8859-1 I agree with 7stud. I tried the method suggested by Peddi before I asked the original question and found that it didn't work. What 7stud pointed out is that I was assuming that variables and objects behave the same way. A name for an object is just a pointer to the value of the object in memory. So when you do something like object1 = object2 all you are doing is setting the pointer for object1 to where object2 is pointing. Not coming from an object orientation for the most part, I ignored that distinction, and didn't get the desired results. This also answers my question of why Ruby was designed this way. Because it is an OO language. Still, for the old timers coming from a COBOL environment, this is a bit of an annoyance. And any instructor needs to be sure that the students understand the significance of variables being objects. They cannot just say, "X is an object" and expect the student to extrapolate. They need to explicitly tell the student how to transfer a value from one object to another. No Sam On Thu, Sep 10, 2009 at 12:20 AM, Yossef Mendelssohn wrote: > On Sep 9, 10:50 pm, venkatesh Peddi wrote: > > the easy way is you can have another temp variable to which u have to > assign the old value.and assing the temp value to new. in this case when u > change the new the temp will change but not old. > > > > temp var = old var > > > > new var = temp var > > > > // here u can do what ever u want on new. unless u directly change any > thing on temp ur old var is safe. > > What are you talking about? Why would that work? Why would there be > any difference between `x = a; b = x` and `b = a`? > > >> a = [1,2,3] > >> x = a > >> b = x > >> b[2] = 4 > >> b > => [1, 2, 4] > >> a > => [1, 2, 4] > > > 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. > > -- > -yossef > > --00235447101495bd2c04733a6a60--