【发布时间】:2015-08-03 21:03:02
【问题描述】:
def gen_path_locations(start, destination)
initial_dest = destination
last_dest = destination
path_locations = []
loop do
last_dest[0] -= 1 if initial_dest[0] > start[0]
last_dest[0] += 1 if initial_dest[0] < start[0]
last_dest[1] -= 1 if initial_dest[1] > start[0]
last_dest[1] += 1 if initial_dest[1] < start[0]
break if last_dest == start
path_locations << last_dest
puts last_dest.inspect
end
path_locations
end
gen_path_locations([5, 5], [9, 9]) 返回[[5, 5], [5, 5], [5, 5]]
它将起始位置添加到我的 path_locations,而不是 last_dest 的当前迭代。当然,如果我把推送改成这样:
path_locations << [last_dest[0], last_dest[1]]
它返回预期值。我错过了什么?
【问题讨论】:
标签: arrays ruby variable-assignment