Recently, I’ve had quite a popular problem to solve: count the occurences of the given item in a collection. There are few ways to solve this problem – starting from using
Enumerable#each_with_object with an empty hash as an accumulator value and writing a code looking like this:
through a bit smarter way and taking advantage of the default hash value:
All these solutions look quite nice; however, there is one that looks particularly beautiful.