Class: Dat::Analysis::Tally
- Inherits:
-
Object
- Object
- Dat::Analysis::Tally
- Defined in:
- lib/dat/analysis/tally.rb
Overview
Internal: Track and summarize counts of occurrences of mismatch objects.
Examples
tally = Dat::Analysis::Tally.new
tally.count('foo')
=> 1
tally.count('bar')
=> 1
tally.count('foo')
=> 2
puts tally.summary
Summary of known mismatches found:
foo 2
bar 1
TOTAL: 3
=> nil
Instance Attribute Summary collapse
-
#tally ⇒ Object
readonly
Public: Returns the hash of recorded mismatches.
Instance Method Summary collapse
-
#count(klass) ⇒ Object
Public: record an occurrence of a mismatch class.
-
#initialize ⇒ Tally
constructor
A new instance of Tally.
-
#summarize ⇒ Object
Public: prints a summary of mismatches seen so far to STDOUT (see ‘#summary` above).
-
#summary ⇒ Object
Public: Return a String summary of mismatches seen so far.
Constructor Details
#initialize ⇒ Tally
Returns a new instance of Tally.
25 26 27 |
# File 'lib/dat/analysis/tally.rb', line 25 def initialize @tally = {} end |
Instance Attribute Details
#tally ⇒ Object (readonly)
Public: Returns the hash of recorded mismatches.
23 24 25 |
# File 'lib/dat/analysis/tally.rb', line 23 def tally @tally end |
Instance Method Details
#count(klass) ⇒ Object
Public: record an occurrence of a mismatch class.
30 31 32 33 |
# File 'lib/dat/analysis/tally.rb', line 30 def count(klass) tally[klass] ||= 0 tally[klass] += 1 end |
#summarize ⇒ Object
Public: prints a summary of mismatches seen so far to STDOUT (see ‘#summary` above).
Returns nil.
55 56 57 |
# File 'lib/dat/analysis/tally.rb', line 55 def summarize puts summary end |
#summary ⇒ Object
Public: Return a String summary of mismatches seen so far.
Returns a printable String summarizing the counts of mismatches seen, sorted in descending count order.
39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/dat/analysis/tally.rb', line 39 def summary return "\nNo results identified.\n" if tally.keys.empty? result = [ "\nSummary of identified results:\n" ] sum = 0 tally.keys.sort_by {|k| -1*tally[k] }.each do |k| sum += tally[k] result << "%30s: %6d" % [k, tally[k]] end result << "%30s: %6d" % ['TOTAL', sum] result.join "\n" end |