#!/usr/bin/ruby

#
## https://rosettacode.org/wiki/Cumulative_standard_deviation
#

class StdDevAccumulator(n=0, sum=0, sumofsquares=0) {
  method <<(num) {
    n += 1;
    sum += num;
    sumofsquares += num**2;
    self;
  }

  method stddev {
    sqrt(sumofsquares/n - pow(sum/n, 2));
  }

  method to_s {
    self.stddev.to_s;
  }
}

var i = 0;
var sd = StdDevAccumulator.new;
[2,4,4,4,5,5,7,9].each {|n|
    say "adding #{n}: stddev of #{i+=1} samples is #{sd << n}"
}
