Module: Enumerable
- Defined in:
- lib/enumeration.rb
Overview
Add methods to Enumerable, which makes them available to Array
Instance Method Summary collapse
-
#average ⇒ Object
average of an array of numbers.
-
#sample_variance ⇒ Object
variance of an array of numbers.
-
#standard_deviation ⇒ Object
standard deviation of an array of numbers.
-
#sum ⇒ Object
sum of an array of numbers.
Instance Method Details
#average ⇒ Object
average of an array of numbers
10 11 12 |
# File 'lib/enumeration.rb', line 10 def average return self.sum/self.length.to_f end |
#sample_variance ⇒ Object
variance of an array of numbers
15 16 17 18 19 |
# File 'lib/enumeration.rb', line 15 def sample_variance avg=self.average sum=self.inject(0){|acc,i|acc +(i-avg)**2} return(1/self.length.to_f*sum) end |
#standard_deviation ⇒ Object
standard deviation of an array of numbers
22 23 24 |
# File 'lib/enumeration.rb', line 22 def standard_deviation return Math.sqrt(self.sample_variance) end |
#sum ⇒ Object
sum of an array of numbers
5 6 7 |
# File 'lib/enumeration.rb', line 5 def sum return self.inject(0){|acc,i|acc +i} end |