Okay,

Take a very close look at the two (one is a class, the other is a

method):

class Stdev

def initialize(arr)

@arr = arr

@size = @arr.to_a.size

end

def sum

@arr.inject {|a,b| a + b }

end

def product

@arr.inject {|a,b| a * b }

end

def calculate

Math.sqrt(((product - (sum * sum)/@size)/(@size-1)))

end

end

def stddev(arr)

size = arr.to_a.size

product, sum = 0.0, 0.0

arr.each do |a|

sum += a

product += a*a

end

std = Math.sqrt(((product - (sum * sum)/size)/(size-1)))

return std

end

Testing with both:

a = [123.369, 68.6179]

Stdev.new(a).calculate

=> Errno::EDOM: Domain error - sqrt

stddev(a)

=> 38.7148740874228

Test a web calculator for the same thing:

http://www.easycalculation.com/statistics/standard-deviation.php

The method will pan out.

So, to save suspense the reason why the class is erroring out is because

the sqrt is being attempted on a negative number. Therefore, in order

to continue to use that I would have to require ‘complex’ and the return

values from complex return two values in an array.

I’m just curious, why, given both of these being so very similar - the

method passes but the class fails?