I have an array [1,2,4,5,4,7] and I want to find the frequency of each number and store it in a hash. I have this code, but it returns NoMethodError: undefine
[1,2,4,5,4,7]
NoMethodError: undefine
Just use inject. This type of application is exactly what it is meant for. Something like:
a.inject(Hash.new(0)) {|hash,word| hash[word] += 1; hash }