我有一个这样的数组
[1,1,2,3,3,3,4,5,5]
我想计算每个数字的出现次数,我正在尝试这样做
[1,1,2,3,3,3,4,5,5].reduce(Hash.new(0)) { |hash,number| hash[number] += 1 }
问题是当我尝试运行它时出现以下错误
NoMethodError: undefined method `[]=' for 1:Fixnum
from (irb):6:in `block in irb_binding'
from (irb):6:in `each'
from (irb):6:in `reduce'
from (irb):6
我可以像这样设置初始值,还是我弄错了?