我想知道計數變量有什麼作用,最後一個結束之前?計數有什麼用途?第7行
# Pick axe page 51, chapter 4
# Count frequency method
def count_frequency(word_list)
counts = Hash.new(0)
for word in word_list
counts[word] += 1
end
counts #what does this variable actually do?
end
puts count_frequency(["sparky", "the", "cat", "sat", "on", "the", "mat"])
+1用於解釋爲什麼在該方法結束時需要「計數」行。 – Teddy
所以它類似於「返回計數」,對吧?如果是這樣,對於初學者來說這很棘手,但很好知道。 – jimmyc3po
@ jimmyc3po正確;在Ruby中,你可以在你的方法的任何地方放置一個顯式的'return'。然而,這並不常見。相反,最後一個表達式的結果通常用作返回值。 – Phrogz