From: "Jesús Gabriel y Galán" Date: 2012-09-28T00:42:43+09:00 Subject: Re: subtract values of a array On Thu, Sep 27, 2012 at 5:36 PM, Roelof Wobben wrote: > Hello, > > I changed from ruby koans to rubymonks. > > Now I have to make a method which can substract any number of numbers. > > So I tried this > > def subtract(*numbers) > numbers.inject(0) { |sum, number| sum - number } > end > > but that won't work subtract ( 4,5) gives -9 when -1 is the right answer. > > Can anyone give me a tip for a better answer. inject(0) will pass 0 as the first accumulator. If you don't pass the 0, then inject will use the first element in the array as the first accumulator, which is what you want I think. Jesus.