From: venkatesh Peddi Date: 2009-09-10T12:50:57+09:00 Subject: Re: How Are Variables Kept Independent of Each Other Yet Pass Values? --_81647f92-5c37-4248-8635-af5eda107a1f_ Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable the easy way is you can have another temp variable to which u have to assig= n the old value.and assing the temp value to new. in this case when u chang= e the new the temp will change but not old. =20 temp var =3D old var new var =3D temp var // here u can do what ever u want on new. unless u directly change any thin= g on temp ur old var is safe. =20 ps:this is a work around=2C i dont recommend it. =20 =20 -V =20 > Date: Thu=2C 10 Sep 2009 12:24:05 +0900 > From: masonkelsey@gmail.com > Subject: How Are Variables Kept Independent of Each Other Yet Pass Values= ? > To: ruby-talk@ruby-lang.org >=20 > Somewhere in the several books I've been learning Ruby from there was the > note that when you create a new variable and assign it a value from anoth= er > variable that you really haven't created an independent variable at all b= ut > just another name for the source variable. So if you code "new_var =3D > old_var" the new_var simply points to the location of the value for > old_var. Consequently=2C if you change one variable=2C you have changed b= oth. > I've tested this and it appears to be true. I've made bold the crucial pa= rt > of the code. >=20 > In the following code verifies that variables are not independent in > assignments: >=20 > # Test to Determine how 2 Dimentional Arrays Work > current_state =3D [[1=2C 3=2C 6]=2C [5=2C 0=2C 2]=2C [4=2C 7=2C 8]] > #new_state =3D [[1=2C 3=2C 6]=2C [5=2C 0=2C 2]=2C [4=2C 7=2C 8]] > *new_state =3D current_state* > puts "Current State =3D " + current_state.to_s > puts "current_state[1][1] is " + current_state[1][1].to_s > puts "current_state[2][1] is " + current_state[2][1].to_s > # Exchange positions > new_state[1][1] =3D current_state[2][1] > new_state[2][1] =3D current_state[1][1] > # Show changed values > puts "New State[1][1] =3D " + new_state[1][1].to_s > puts "New State[2][1] =3D " + new_state[2][1].to_s >=20 > Gives the results: >=20 > >ruby Simple_Test_Class_EightPuzzle.rb > Current State =3D 136502478 > current_state[1][1] is 0 *<=3D=3D Values before the change* > current_state[2][1] is 7 *<=3D=3D Values before the change* > *New State[1][1] =3D 7 <=3D=3D Values after the change > New State[2][1] =3D 7 <=3D=3D Values after the change. Due to new_state a= nd > current_state pointing to the same value. > *>Exit code: 0 >=20 > Which would only happen if new_state and current_state point to the same > value! >=20 > If I execute the code with the above bold line commented out and the comm= ent > line above it executed=2C then the exchange occurs properly because new_s= tate > and current_state are two independent variables. Thus=2C >=20 > # Test to Determine how 2 Dimentional Arrays Work > current_state =3D [[1=2C 3=2C 6]=2C [5=2C 0=2C 2]=2C [4=2C 7=2C 8]] > *new_state =3D [[1=2C 3=2C 6]=2C [5=2C 0=2C 2]=2C [4=2C 7=2C 8]] > *#new_state =3D current_state > puts "Current State =3D " + current_state.to_s > puts "current_state[1][1] is " + current_state[1][1].to_s > puts "current_state[2][1] is " + current_state[2][1].to_s > # Exchange positions > new_state[1][1] =3D current_state[2][1] > new_state[2][1] =3D current_state[1][1] > # Show changed values > puts "New State[1][1] =3D " + new_state[1][1].to_s > puts "New State[2][1] =3D " + new_state[2][1].to_s >=20 > produces the desired results=2C as >=20 > >ruby Simple_Test_Class_EightPuzzle.rb > Current State =3D 136502478 > current_state[1][1] is 0 > current_state[2][1] is 7 > *New State[1][1] =3D 7 > New State[2][1] =3D 0 <=3D=3D Proper exchange occurred because new_state = and > current_state are independent. > *>Exit code: 0 > Now=2C my problem is that I *must* give new_state the value that current_= state > has but keep them independent=2C because I need to save the original > configuration of values. How do I pass a value to a new variable from a > source variable yet keep them independent? I'm sure there is a way=2C I j= ust > cannot remember where I read it in the books and the indexes are not > helpful. >=20 > Thanks in advance! >=20 > No Sam _________________________________________________________________ Sports=2C news=2C fashion and entertainment. Pick it all up in a package ca= lled MSN India http://in.msn.com= --_81647f92-5c37-4248-8635-af5eda107a1f_--