Class: Tailstrom::Counter
- Inherits:
-
Object
- Object
- Tailstrom::Counter
- Defined in:
- lib/tailstrom/counter.rb
Instance Method Summary collapse
- #<<(value) ⇒ Object
- #avg ⇒ Object
- #clear ⇒ Object
- #count ⇒ Object
-
#initialize ⇒ Counter
constructor
A new instance of Counter.
- #max ⇒ Object
- #med ⇒ Object
- #min ⇒ Object
- #purge_cache ⇒ Object
- #sum ⇒ Object
Constructor Details
#initialize ⇒ Counter
Returns a new instance of Counter.
3 4 5 |
# File 'lib/tailstrom/counter.rb', line 3 def initialize clear end |
Instance Method Details
#<<(value) ⇒ Object
7 8 9 10 |
# File 'lib/tailstrom/counter.rb', line 7 def <<(value) purge_cache @values << value end |
#avg ⇒ Object
21 22 23 24 |
# File 'lib/tailstrom/counter.rb', line 21 def avg return nil if @values.empty? sum / @values.length end |
#clear ⇒ Object
12 13 14 15 |
# File 'lib/tailstrom/counter.rb', line 12 def clear @values = [] purge_cache end |
#count ⇒ Object
42 43 44 |
# File 'lib/tailstrom/counter.rb', line 42 def count @cache[:count] ||= @values.count end |
#max ⇒ Object
34 35 36 |
# File 'lib/tailstrom/counter.rb', line 34 def max @cache[:max] ||= @values.max end |
#med ⇒ Object
38 39 40 |
# File 'lib/tailstrom/counter.rb', line 38 def med @values[@values.length / 2] end |
#min ⇒ Object
30 31 32 |
# File 'lib/tailstrom/counter.rb', line 30 def min @cache[:min] ||= @values.min end |
#purge_cache ⇒ Object
17 18 19 |
# File 'lib/tailstrom/counter.rb', line 17 def purge_cache @cache = {} end |
#sum ⇒ Object
26 27 28 |
# File 'lib/tailstrom/counter.rb', line 26 def sum @cache[:sum] ||= @values.inject(0, :+) end |