Issue #11076 has been updated by Harald Bttiger.


I am sorry but I am not sure to properly format this, but the documentationwould be like:

    Syntax:
      group_by { |obj| block } вк a_hash
      group_by вк an_enumerator
    
    Description:
      Groups the collection by result of the block. Returns a hash where the keys are the evaluated result from the block and the values are the number of the elements in the collection that correspond to the key.
    
      If no block is given an enumerator is returned.
    
    Examples:
      ['a','a','a','b','c'].group_by { |x| x } #=> {'a'=>3, 'b'=>1, 'c'=>1}
      (1..7).group_by { |i| i%3 }   #=> {0=>2, 1=>3, 2=>2}


----------------------------------------
Feature #11076: Enumerable method count_by
https://bugs.ruby-lang.org/issues/11076#change-52204

* Author: Harald Bttiger
* Status: Open
* Priority: Low
* Assignee: 
----------------------------------------
I very often use `Hash[array.group_by{|x|x}.map{|x,y|[x,y.size]}]`.

Would be nice with to have a method called `count_by`:

~~~ruby
array = ['aa', 'aA', 'bb', 'cc']
p array.count_by(&:downcase) #=> {'aa'=>2,'bb'=>1,'cc'=>1}
~~~



-- 
https://bugs.ruby-lang.org/