From: Chuck Remes Date: 2009-07-21T22:28:11+09:00 Subject: Re: Mean method On Jul 21, 2009, at 7:41 AM, Älphä Blüë wrote: > I'm working on a lot of math in my projects so I thought I would > convert > over a few methods I used in other languages that I created and port > them to ruby. > > Here's a mean method which allows you to return either: > > a) arithmetic mean > b) geometric mean > c) harmonic mean > > http://pastie.org/553287 The pastie is missing your extension of Array to add the #sum method, so the example doesn't work. Also, unless #sum returns a float then it is possible for your arithmetic mean to return a result truncated to the nearest integer due to the way Ruby's division works with integers. Lastly, I have a style suggestion. In your parameter sanity checks, I think this reads easier: raise "mean(#{an_array}) error: #{an_array} is not an array." unless an_array.is_a? Array raise "mean(#{an_array},#{i_type}) error:: #{i_type} mean type is not an integer." unless i_type.is_a? Fixnum raise "mean(#{an_array},#{i_type}) error: invalid mean type ((#{i_type}) set. Only 1..3 valid." unless (1..3).include? i_type Thanks for sharing your code. cr