Module: Enumerable

Defined in:
lib/golly-utils/ruby_ext/enumerable.rb

Instance Method Summary collapse

Instance Method Details

#frequency_mapHash<Object,Fixnum>

Creates a map of all elements by the frequency that they occur.

Examples:

%w[big house big car].frequency_map  # => {"big"=>2, "car"=>1, "house"=>1}

Returns:

  • (Hash<Object,Fixnum>)

    Map of element to frequency.



9
10
11
12
13
14
# File 'lib/golly-utils/ruby_ext/enumerable.rb', line 9

def frequency_map
  inject Hash.new do |h,e|
    h[e]= (h[e] || 0) + 1
    h
  end
end