4 ms·
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"=>
by mattmarcus 7y 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-a706a5fb11ea 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 }
- meowface 7y agoFor Python developers: this exists as collections.Counter.
- Twirrim 7y agoI 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.
- faitswulff 7y agoI was a fan of list.group_by(&:itself).transform_values(&:count) ...but I'll be very happy to replace that with #tally!