Module: MoreCoreExtensions::ArrayElementCounts
- Defined in:
- lib/more_core_extensions/core_ext/array/element_counts.rb
Instance Method Summary collapse
- 
  
    
      #element_counts  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    Returns a Hash of each element to the count of those elements. 
Instance Method Details
#element_counts ⇒ Object
Returns a Hash of each element to the count of those elements. Optionally
pass a block to count by a different criteria.
 [1, 2, 3, 1, 3, 1].counts  # => {1 => 3, 2 => 1, 3 => 2}
 %w(a aa aaa a aaa a).counts { |i| i.length }  # => {1 => 3, 2 => 1, 3 => 2}
| 9 10 11 12 13 14 | # File 'lib/more_core_extensions/core_ext/array/element_counts.rb', line 9 def element_counts each_with_object(Hash.new(0)) do |i, h| key = block_given? ? yield(i) : i h[key] += 1 end end |