這個問題在這里已經有了答案: 關于 Ruby << 運算子的澄清 5 個答案 昨天關門。
我對<<操作員的作業感到困惑。
def reconstruct_path(prev, current)
if prev.include? current
p = reconstruct_path(prev, prev[current])
[p] << current
else
current
end
end
uj5u.com熱心網友回復:
Array#<<被稱為“鏟子”。它將“事物”推入其他“事物”。
在這個例子中就像這樣:
thing_array = [1, 2, 3, 4, 5]
thing_array << 6
Result: => [1, 2, 3, 4, 5, 6]
所以陣列thing_array是用數字 1 到 5 創建的。通過使用鏟子,它將新條目推6入thing_array. 這只是讓代碼更簡潔、更輕便的捷徑。
我希望這是有道理的。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/497650.html
標籤:红宝石
下一篇:Ruby用單黑斜線替換雙黑斜線
