Module: Enumerable

Defined in:
lib/count_by.rb

Instance Method Summary collapse

Instance Method Details

#count_by(&block) ⇒ Object

Raises:

  • (ArgumentError)


2
3
4
5
6
7
8
# File 'lib/count_by.rb', line 2

def count_by(&block)
  raise ArgumentError, "count_by: block not specified" unless block_given?

  each_with_object(Hash.new(0)) do|elm, h|
    h[yield(elm)] += 1
  end
end