From: rashworth Date: 2001-03-13T06:10:35+09:00 Subject: [ruby-talk:12529] Re: Sum of Squares Thank you for your note. The new coding worked just fine. Do you have some code for the Sum of the Cross-Products? Sumxy = (x1 * y1) + (x2 * y2) + .... Bob Ashworth ---------- Original Message ---------------------------------- From: Mathieu Bouchard Reply-To: ruby-talk@ruby-lang.org Date: Mon, 12 Mar 2001 07:48:05 +0900 >On Mon, 12 Mar 2001, rashworth wrote: >> # The following code does not Sum the squares of the elements. >> # Define class Array for Sum of squares >> class Array >> def inject(n) >> each { |value| n = yield(n, value) } >> n > >"nested method definition". you forgot an "end" here. > >> def sumsq >> inject(0) { | n, value | (n * n) + value } > >this should be > inject(0) { | n, value | n + value*value } > >matju > >