我明白「count」方法的工作原理,從The string count() method。計數如何在Ruby中工作
但我不明白它如何在陣列計算的話(而不是字母):
def find_frequency(sentence, word)
sentence.downcase.split.count(word.downcase)
end
find_frequency("to be or not to be", "to") # => 2
# same as ["to", "be", "or", "not", "to", "be"].count("to")
"hello world".count("lo") # => 5
如果"hello world".count("lo")
返回五,爲什麼不find_frequency("to be or not to be", "to")
收益七(T,O,O,O, t,t,o)?
,謝謝!特別是鏈接到[apidock](http://apidock.com)。 – Evgeny 2013-05-01 07:57:23
你可以加入這裏 - http://chat.stackoverflow.com/rooms/27184/ruby-conceptual? – 2013-05-01 10:29:25