# 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
def sumsq
inject(0) { | n, value | (n * n) + value }
end
end
[ 1, 2 ].sumsq # 1 + 4 = 5
[ 1, 1, 0, 1 ].sumsq # 1 + 1 + 0 + 1 = 3
# Can you tell me what the correct code should be?
Thank you very much.
Bob Ashworth