From: Robert Klemme Date: 2008-03-27T03:34:55+09:00 Subject: Re: The inject function On 26.03.2008 15:32, Eustáquio 'TaQ' Rangel wrote: > You could use a test to sum the number only when it's even on inject, but > > [1,2,3,4,5,6].inject {|m,v| m + (v%2==0?v:0) } > > results to 13, because seems that the value of m is automatically the first > element of the array. On this case, you can tell it to start with 0: > > [1,2,3,4,5,6].inject(0) {|m,v| m + (v%2==0?v:0) } Actually these two are not equivalent! You should usually use the second form because the first form will return nil for an empty Array. Btw, Radoslav's solution omits unnecessary additions of 0: [1,2,3,4,5,6].inject(0) {|m,v| v % 2 == 0 ? m : m+v } > | Second, I need to reverse an array using only the inject command. So, > | something like above with the result [4,3,2,1] > > Why don't you use reverse? I assume it is some kind of task assignment - which is why I regret that solutions were posted so quickly. Kind regards robert