Module: Enumerable
- Defined in:
- lib/gci-class-extensions.rb
Overview
Get the counts in an enumerable based on various criteria ex. ‘[1, 2, 3].count_by(&:even?)` should return `=> 1, false => 2`
Instance Method Summary collapse
Instance Method Details
#count_by(&block) ⇒ Object
35 36 37 38 39 40 41 42 43 |
# File 'lib/gci-class-extensions.rb', line 35 def count_by(&block) counts = {} grouped = group_by(&block) grouped.each do |key, vals| counts[key] = vals.size end counts end |