--_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 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. ps:this is a work around, i dont recommend it. -V > Date: Thu, 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 > > 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 another > variable that you really haven't created an independent variable at all but > just another name for the source variable. So if you code "new_var = > old_var" the new_var simply points to the location of the value for > old_var. Consequently, if you change one variable, you have changed both. > I've tested this and it appears to be true. I've made bold the crucial part > of the code. > > In the following code verifies that variables are not independent in > assignments: > > # Test to Determine how 2 Dimentional Arrays Work > current_state = [[1, 3, 6], [5, 0, 2], [4, 7, 8]] > #new_state = [[1, 3, 6], [5, 0, 2], [4, 7, 8]] > *new_state = current_state* > puts "Current State = " + 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] = current_state[2][1] > new_state[2][1] = current_state[1][1] > # Show changed values > puts "New State[1][1] = " + new_state[1][1].to_s > puts "New State[2][1] = " + new_state[2][1].to_s > > Gives the results: > > >ruby Simple_Test_Class_EightPuzzle.rb > Current State = 136502478 > current_state[1][1] is 0 *<== Values before the change* > current_state[2][1] is 7 *<== Values before the change* > *New State[1][1] = 7 <== Values after the change > New State[2][1] = 7 <== Values after the change. Due to new_state and > current_state pointing to the same value. > *>Exit code: 0 > > Which would only happen if new_state and current_state point to the same > value! > > If I execute the code with the above bold line commented out and the comment > line above it executed, then the exchange occurs properly because new_state > and current_state are two independent variables. Thus, > > # Test to Determine how 2 Dimentional Arrays Work > current_state = [[1, 3, 6], [5, 0, 2], [4, 7, 8]] > *new_state = [[1, 3, 6], [5, 0, 2], [4, 7, 8]] > *#new_state = current_state > puts "Current State = " + 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] = current_state[2][1] > new_state[2][1] = current_state[1][1] > # Show changed values > puts "New State[1][1] = " + new_state[1][1].to_s > puts "New State[2][1] = " + new_state[2][1].to_s > > produces the desired results, as > > >ruby Simple_Test_Class_EightPuzzle.rb > Current State = 136502478 > current_state[1][1] is 0 > current_state[2][1] is 7 > *New State[1][1] = 7 > New State[2][1] = 0 <== Proper exchange occurred because new_state and > current_state are independent. > *>Exit code: 0 > Now, my problem is that I *must* give new_state the value that current_state > has but keep them independent, 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, I just > cannot remember where I read it in the books and the indexes are not > helpful. > > Thanks in advance! > > No Sam _________________________________________________________________ Sports, news, fashion and entertainment. Pick it all up in a package called MSN India http://in.msn.com-_81647f92-5c37-4248-8635-af5eda107a1f_--