From: Rob Biedenharn Date: 2008-04-26T01:47:26+09:00 Subject: Re: Array inject function problem On Apr 25, 2008, at 12:20 PM, Inbroker Adams wrote: > Thanks so much guys. > So the sum =[1,3,5].inject() {|sum, element| sum + element;} > runs on both 1.8 and 1.9 right??? > 1.9 Ruby is in beta? (haven't seen it yet) > not on the official site neither on the netbeans 6.1 RC2 that i just > installed Yes, but if the array is empty [] rather than 0 you'd get nil. If you don't want that behavior, provide the initial value for the accumulator as an argument: sum = [].inject(0) {|sum, element| sum + element} gives sum == 0 -Rob Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com