我正在尝试计算数组中元素的出现次数并将其保存在哈希中。我想使用注入功能。我有这个代码:
a = ["the","the","a", "it", "it", "it"]
a.inject(Hash.new(0)) {|hash,word| hash[word] += 1}
我不明白为什么会出现以下错误:
TypeError: can't convert String into Integer
from (irb):47:in `[]'
from (irb):47:in `block in irb_binding'
from (irb):47:in `each'
from (irb):47:in `inject'
另外,我不知道如何解决它。