class CodeHistogram

Keep track of the count of occurences of things (in code)

Attributes

counts[R]

Public Class Methods

new() click to toggle source
# File lib/zombie_killer/code_histogram.rb, line 7
def initialize
  @counts = Hash.new do |hash, key|
    hash[key] = 0
  end
end
parse_by_frequency(lines) click to toggle source
# File lib/zombie_killer/code_histogram.rb, line 27
def self.parse_by_frequency(lines)
  histogram = CodeHistogram.new
  lines.each do |line|
    /^\s*(\d*)\s*(.*)/.match(line.chomp) do |m|
      histogram.increment(m[2], m[1].to_i)
    end
  end
  histogram
end

Public Instance Methods

increment(key, value = 1) click to toggle source
# File lib/zombie_killer/code_histogram.rb, line 13
def increment(key, value = 1)
  @counts[key] += value
end
merge!(other) click to toggle source
# File lib/zombie_killer/code_histogram.rb, line 37
def merge!(other)
  counts.merge!(other.counts) do |_key, count, other_count|
    count + other_count
  end
end
print_by_frequency(io) click to toggle source

Private Instance Methods

invert_hash_preserving_duplicates(h) click to toggle source
# File lib/zombie_killer/code_histogram.rb, line 45
def invert_hash_preserving_duplicates(h)
  ih = {}
  h.each do |k, v|
    ih[v] = [] unless ih.key?(v)
    ih[v] << k
  end
  ih
end