This page contains automated test results for code from O'Reilly's Ruby Cookbook. If this code looks interesting or useful, you might want to buy the whole book.

Summing Each Item of an Array
CodeExpectedActual
collection = [1, 2, 3, 4, 5]
sum = 0
collection.each {|i| sum += i}
sum
15 15
collection = [1, 2, 3, 4, 5]
collection.inject(0) {|sum, i| sum + i}
15 15
collection = [1, 2, 3, 4, 5]
sum = 0
sum = sum + 1
sum = sum + 2
sum = sum + 3
sum = sum + 4
sum = sum + 5
collection = [1, 2, 3, 4, 5]
collection.inject(1) {|total, i| total * i}
120 120