From: Rick DeNatale Date: 2008-06-12T20:38:10+09:00 Subject: Re: Array inject function problem ------=_Part_9631_25107187.1213270715403 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline On Thu, Jun 12, 2008 at 3:27 AM, Robert Dober wrote: > On Thu, Jun 12, 2008 at 9:15 AM, Sebastian Hungerecker > wrote: > > Rick DeNatale wrote: > >> [1,3,5].inject([]) {|s,e| s = s.to_a; s << e + ( s.last || 0)} > > > > If you're okay with 0 being inside the result array you can also do: > > [1,3,5].inject([0]) {|s, e| s << s.last + e} > > and if you are not > > [1,3,5].inject([0]){| a, e| a<< a.last + e}[1..-1] Which I had in my original offering before Sebastien trimmed it. -- Rick DeNatale My blog on Ruby http://talklikeaduck.denhaven2.com/ ------=_Part_9631_25107187.1213270715403--