Class: Huff::FrequencyMeter

Inherits:
Object
  • Object
show all
Defined in:
lib/huff/frequency_meter.rb

Instance Method Summary collapse

Constructor Details

#initialize(text) ⇒ FrequencyMeter

Returns a new instance of FrequencyMeter.



3
4
5
# File 'lib/huff/frequency_meter.rb', line 3

def initialize(text)
  @text = text
end

Instance Method Details

#frequencyObject



7
8
9
10
11
12
13
# File 'lib/huff/frequency_meter.rb', line 7

def frequency
  result = Hash.new { |h, k| h[k] = 0 }
  @text.each_char.
      each_with_object(result) { |char, hash| hash[char] += 1 }.
      map { |c, f| [f, c] }.
      sort { |(f1, _), (f2, _)| f1 <=> f2 }
end