jemnotesversion 2 / featuring this entry or see all/search

Dec 10
Another reason I love ruby: say I have an array of items (a ‘list’, in python) and want to convert it to a hash (python: ‘dictionary’) giving the frequency of each item. I can do it like this:
def freq_table(a)
  d = {}
  d.default = 0
  a.each{|x| d[x] += 1}
  d
end
(Yep, that’s all you need!)