Pastie now auto-senses if line-wrap is a bad or good idea. Feedback?
## mark a section (Learn more)
This paste will be private.
class Array def errorcheck(name) raise "The #{name} could not be calculated for this array: Empty Array" if self.empty? #raise "The #{name} could not be calculated for this array: Non-numeric Array" if self.each {|value| !value.is_a?(Numeric)} end def sum errorcheck("sum") sum = 0 self.each {|value| sum+=value} return sum end def mean errorcheck("mean") return (self.sum).to_f/self.size end def stdev errorcheck("stdev") temp = 0 self.each {|value| temp+=((value - self.mean)**2)} Math.sqrt(temp/self.size) end def median errorcheck("median") array = self.sort pos = array.size / 2 return array.size % 2 == 1 ? array[pos] : (array[pos-1..pos]).mean end def range errorcheck("range") minval = self.min maxval = self.max return "Range: #{minval} ~ #{maxval}" end end regnum = [3, "gay", 7, 19] floatnum = [5.5, 5, 10, 20] regempty = [] tempx = ["x", "y"] puts floatnum.stdev
From the Design Piracy series on my blog: