Module: Enumerable

Defined in:
lib/maths.rb

Overview

Add methods to Enumerable, which makes them available to Array

Instance Method Summary collapse

Instance Method Details

#meanObject

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_varianceObject

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_deviationObject

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

#sumObject

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