From: Stefano Crocco Date: 2012-09-28T00:42:16+09:00 Subject: Re: subtract values of a array On Friday 28 September 2012 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. > > Roelof Do you mean you want a method which subtracts some numbers from a given one (for example: subtract(10,3,4) should give 10 - 3 - 4)? In this case, I'd say you need a required parameter, which is the number to subtract from and which you pass as first argument to inject. To see why your method doesn't do what you want, check the documentation for inject and, in particular, what the parameter means. I hope this helps Stefano