我收到一个错误,即 nil:NilClass 未定义“+”。我假设这是在
index[word] += 1
但不知道为什么。我正在使用 1.9.3。
如果有人可以提供帮助,将不胜感激!谢谢
def most_common_words(text)
text_split = text.split(' ')
index = {}
text_split.each do |word|
puts index
puts word
if (index[word]
index[word] += 1 )
else(
index[word] = 1 )
end
end
index.to_a.sort[0..2]