Class: RateCounter
- Inherits:
-
Object
- Object
- RateCounter
- Defined in:
- lib/ratecounter.rb
Instance Attribute Summary collapse
-
#sampledata ⇒ Object
Returns the value of attribute sampledata.
-
#samples ⇒ Object
Returns the value of attribute samples.
Instance Method Summary collapse
-
#initialize(samples = 50) ⇒ RateCounter
constructor
A new instance of RateCounter.
- #rate ⇒ Object
- #tick ⇒ Object
Constructor Details
#initialize(samples = 50) ⇒ RateCounter
Returns a new instance of RateCounter.
5 6 7 8 |
# File 'lib/ratecounter.rb', line 5 def initialize(samples = 50) self.samples = samples self.sampledata = Array.new end |
Instance Attribute Details
#sampledata ⇒ Object
Returns the value of attribute sampledata.
3 4 5 |
# File 'lib/ratecounter.rb', line 3 def sampledata @sampledata end |
#samples ⇒ Object
Returns the value of attribute samples.
3 4 5 |
# File 'lib/ratecounter.rb', line 3 def samples @samples end |
Instance Method Details
#rate ⇒ Object
17 18 19 20 21 |
# File 'lib/ratecounter.rb', line 17 def rate numSeconds = self.sampledata.last - self.sampledata.first return 0 if self.sampledata.length == 0 || numSeconds == 0 return self.sampledata.length.to_f / numSeconds.to_f end |
#tick ⇒ Object
10 11 12 13 14 15 |
# File 'lib/ratecounter.rb', line 10 def tick self.sampledata << Time.now while (self.sampledata.length > 50) do self.sampledata.delete(self.sampledata.first) end end |