lib/nrser/ext/enumerable.rb
# Count entries by the value returned when they are passed to the block.
#
# @example Count array entries by class
#   require 'nrser/ext/enumerable'
#   
#   [1, 2, :three, 'four', 5, :six].n_x.count_by &:class
#   #=> {Fixnum=>3, Symbol=>2, String=>1}
# 
# @param [Proc<(E)=>C>] block
#   Block mapping entries to the group to count them in.
# 
# @return [Hash{C=>Integer}]
#   Hash mapping groups to positive integer counts.
# 
Count entries by the value returned when they are passed to the block.
Count entries by the value returned when they are passed to the block .
def count_by &block
    each_with_object( ::Hash.new 0 ) do |entry, hash|
      hash[ block.call entry ] += 1
    end
  end
def count_by & block each_with_object ( :: Hash . new 0 ) do | entry , hash | hash [ block . call entry ] += 1 end end
Count entries by the value returned when they are passed to the block.
Count entries by the value returned when they are passed to the block .
Hash mapping groups to positive integer counts.
Hash mapping groups to positive integer counts .
Hash{C=>Integer}
block
<not_specific>
<not_specific>
block Block mapping entries to the group to count them in.
block Block mapping entries to the group to count them in .
example
Count array entries by class require 'nrser/ext/enumerable' [1, 2, :three, 'four', 5, :six].n_x.count_by &:class #=> {Fixnum=>3, Symbol=>2, String=>1}
Count array entries by class require ' nrser / ext / enumerable ' [ 1 2 : three ' four ' 5 : six ] . n_x . count_by & : class # = > { Fixnum = > 3 Symbol = > 2 String = > 1 }