I'm not a big fan of adding methods to Enumerable , as there may be unwanted side effects. It also provides methods specific to an array of numbers for any class that inherits from Enumerable , which in most cases does not make sense.
While this is good for tests, scripts, or small applications, it is risky for larger applications, so the alternative here is based on @tolitius answer, which was already perfect. This is more for reference than anything else:
module MyApp::Maths def self.sum(a) a.inject(0){ |accum, i| accum + i } end def self.mean(a) sum(a) / a.length.to_f end def self.sample_variance(a) m = mean(a) sum = a.inject(0){ |accum, i| accum + (i - m) ** 2 } sum / (a.length - 1).to_f end def self.standard_deviation(a) Math.sqrt(sample_variance(a)) end end
And then you use it as such:
2.0.0p353 > MyApp::Maths.standard_deviation([1,2,3,4,5]) => 1.5811388300841898 2.0.0p353 :007 > a = [ 20, 23, 23, 24, 25, 22, 12, 21, 29 ] => [20, 23, 23, 24, 25, 22, 12, 21, 29] 2.0.0p353 :008 > MyApp::Maths.standard_deviation(a) => 4.594682917363407 2.0.0p353 :043 > MyApp::Maths.standard_deviation([1,2,2.2,2.3,4,5]) => 1.466628787389638
The behavior is the same, but it avoids the overhead and risks of adding methods to Enumerable .