M
Mason Kelsey
[Note: parts of this message were removed to make it a legal post.]
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:
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
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
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:
Current State = 136502478ruby Simple_Test_Class_EightPuzzle.rb
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
Current State = 136502478ruby Simple_Test_Class_EightPuzzle.rb
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