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]]
Он возвращает ожидаемые значения. Что мне не хватает?