Hacker News new | ask | show | jobs
by mattmarcus 2374 days ago
I'm most excited for Enumerable#tally for counting occurrences of elements.

From the example in the release:

  ["a", "b", "c", "b"].tally
  #=> {"a"=>1, "b"=>2, "c"=>1}
There's more about this change here (https://medium.com/@baweaver/ruby-2-7-enumerable-tally-a706a...). I probably do this a few times a week:

  list.each_with_object(Hash.new(0)) { |v, h| h[v.something] += 1 }
2 comments

For Python developers: this exists as collections.Counter.
I stumbled across collections.Counter during last year's Advent of Code, and promptly kicked myself for the number of times I'd implmeented that by hand.
I was a fan of

    list.group_by(&:itself).transform_values(&:count)
...but I'll be very happy to replace that with #tally!