我有一段代碼存在問題。具有相同對象ID的Ruby變量
temp_state = @state
我想要做的就是我的實例變量@state的值賦給新的局部變量temp_state。問題是,當我做
temp_state.object_id = 70063255838500
state.object_id = 70063255838500
當我修改temp_state我也修改@狀態。如何在不修改@state的內容的情況下使用temp_state?
這裏有階級的重要組成部分:
class SearchNode
attr_accessor :state, :g, :f, :free_index
def initialize(state, parent = self)
@state = state
@g = parent == self ? 1 : parent.g
@h = calculate_h
@f = @g + @h
@valid_action = ["Move(Black)", "Move(Red)", "Jump(Black)", "Jump(Red)"]
@free_index = index_of_free
@parent = parent
end
def move_black_jump
free = @free_index
# PROBLEM NEXT LINE
temp_state = @state
if temp_state[free + 2] == 'B' || temp_state[free - 2] == 'B'
if free - 2 >= 0 && free + 2 <= temp_state.length
index = free - 2 if temp_state[free - 2] == 'B'
index = free + 2 if temp_state[free + 2] == 'B'
else
puts "ERROR: Movement out of bounds."
end
x = temp_state[index]
temp_state[index] = 'F'
temp_state[free] = x
else
puts "ERROR: Wrong movement move_black_jump."
end
return temp_state
end
end
謝謝您的幫助。
至少有兩個答案指出'dup'使接收器的'淺'副本。如果您或其他讀者不瞭解限定詞「淺」的意義,這兩個示例可能會有所幫助:'a = [1,2,3]; b = a.dup#=> [1,2,3]; a [1] ='cat'#a => [1,「cat」,3],b => [1,2,3]'但如果'a = [1,[2,3]]; b = a.dup#=> [1,[2,3]];一個[0] = '狗'; a [1] ='貓'#a => [「狗」,[2,「貓」],b => [1,[2,「貓」]]。 –