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