0
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]]
它返回期望值。我錯過了什麼?