I'm trying to count the occurrences of elements in an array and save it in a hash. I'd like to use the inject function. I have this code:
a = ["the","the","a", "it", "it", "it"]
a.inject(Hash.new(0)) {|hash,word| hash[word] += 1}
I don't understand why I get the following error:
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'
Also, I don't know how to fix it.