您的位置:首页 > 其它

using inject to count elements in array

2014-06-25 15:29 453 查看
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.

inject
calls your block with two parameters, memo and current element. It then takes return value of the block and
replaces memo with it. Your block returns integers. So, after first iteratation, your memo is not a hash anymore, it's an integer. And integers don't accept strings in their indexers.

Fix is easy, just return hash from the block.

a = ["the","the","a", "it", "it", "it"]
a.inject(Hash.new(0)) {|hash,word| hash[word] += 1; hash }

You may prefer
each_with_object
because it doesn't replace memo. Note that
each_with_object
accepts parameters in reverse (element first, memo second).

a.each_with_object(Hash.new(0)) {|word, hash| hash[word] += 1}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐