Module: Enumerable
- Defined in:
- lib/summarytools/core_extensions/enumerable/sd.rb,
lib/summarytools/core_extensions/enumerable/sum.rb,
lib/summarytools/core_extensions/enumerable/mean.rb,
lib/summarytools/core_extensions/enumerable/median.rb
Instance Method Summary collapse
-
#mean(default = nil) ⇒ Object
Calculates the mean of a numeric collection.
- #median(default = nil) ⇒ Object
-
#sd ⇒ Object
def variance.
-
#sum(identify = nil) ⇒ Object
Sums up elements of a collection by invoking their ‘+` method.
- #variance ⇒ Object
Instance Method Details
#mean(default = nil) ⇒ Object
Calculates the mean of a numeric collection.
14 15 16 17 |
# File 'lib/summarytools/core_extensions/enumerable/mean.rb', line 14 def mean(default = nil) coll_size = to_a.size coll_size > 0 ? reduce(&:+) / coll_size.to_f : default end |
#median(default = nil) ⇒ Object
3 4 5 6 |
# File 'lib/summarytools/core_extensions/enumerable/median.rb', line 3 def median(default = nil) sorted = sort (sorted[(length - 1) / 2] + sorted[length / 2]) / 2.0 end |
#sd ⇒ Object
def variance
8 9 10 |
# File 'lib/summarytools/core_extensions/enumerable/sd.rb', line 8 def sd Math.sqrt(variance) end |
#sum(identify = nil) ⇒ Object
Sums up elements of a collection by invoking their ‘+` method.
15 16 17 |
# File 'lib/summarytools/core_extensions/enumerable/sum.rb', line 15 def sum(identify = nil) reduce(&:+) || default end |
#variance ⇒ Object
3 4 5 6 |
# File 'lib/summarytools/core_extensions/enumerable/sd.rb', line 3 def variance sum = inject(0) { |accum, i| accum + (i - mean) ** 2} sum/(length - 1).to_f end |