Class: Continuance::Durations
- Inherits:
-
Object
- Object
- Continuance::Durations
- Defined in:
- lib/continuance/durations.rb
Overview
Durations takes a collection of duration objects on which some standard collection operations can be performed such as average etc
Instance Attribute Summary collapse
-
#items ⇒ Object
readonly
Returns the value of attribute items.
Instance Method Summary collapse
-
#average ⇒ Object
(also: #arithmetic_mean, #median)
Given a series of durations, this function calculates the average duration by converting each duration to an absolute float value.
-
#initialize(items = []) ⇒ Durations
constructor
Constructor can take a sequence of duration objects, objects outside of durations are ignored.
-
#max ⇒ Object
Tells us what the largest duration is of the given ones.
-
#min ⇒ Object
Tells us what the smallest duration is of the given ones.
-
#standard_deviation ⇒ Object
The standard deviation is simply the square root of the variance.
-
#total ⇒ Object
Provides a sum of all the durations.
-
#variance ⇒ Object
TODO: Add a variance and standard deviation methods to this class.
Constructor Details
#initialize(items = []) ⇒ Durations
Constructor can take a sequence of duration objects, objects outside of durations are ignored
10 11 12 |
# File 'lib/continuance/durations.rb', line 10 def initialize(items = []) @items = items end |
Instance Attribute Details
#items ⇒ Object (readonly)
Returns the value of attribute items.
6 7 8 |
# File 'lib/continuance/durations.rb', line 6 def items @items end |
Instance Method Details
#average ⇒ Object Also known as: arithmetic_mean, median
Given a series of durations, this function calculates the average duration by converting each duration to an absolute float value
16 17 18 19 |
# File 'lib/continuance/durations.rb', line 16 def average time = Time.at(avg_as_f).utc.strftime('%H:%M:%S:%N') Duration.create(time, '%H:%M:%S:%L') end |
#max ⇒ Object
Tells us what the largest duration is of the given ones
39 40 41 |
# File 'lib/continuance/durations.rb', line 39 def max items.map(&:to_f).max end |
#min ⇒ Object
Tells us what the smallest duration is of the given ones
34 35 36 |
# File 'lib/continuance/durations.rb', line 34 def min items.map(&:to_f).min end |
#standard_deviation ⇒ Object
The standard deviation is simply the square root of the variance
56 57 58 |
# File 'lib/continuance/durations.rb', line 56 def standard_deviation Math.sqrt(variance) end |
#total ⇒ Object
Provides a sum of all the durations
27 28 29 30 31 |
# File 'lib/continuance/durations.rb', line 27 def total items.inject(0.0) do |sum, duration| sum += duration.to_f if duration.respond_to?(:to_f) end end |
#variance ⇒ Object
TODO: Add a variance and standard deviation methods to this class
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/continuance/durations.rb', line 44 def variance if items.empty? 0 else mean = average # Cache the average for future use diffs = items.map { |duration| duration.to_f - mean.to_f } diff_squares = diffs.map { |diff| diff**2 } diff_squares.inject(:+) / items.size end end |