From: Ross Bamford Date: 2006-04-25T02:40:25+09:00 Subject: Re: How to make an array from a date range? On Tue, 2006-04-25 at 01:42 +0900, Logan Capaldo wrote: > On Apr 23, 2006, at 1:11 PM, Robin Stocker wrote: > > > ara.t.howard@noaa.gov wrote: > >> if you do simply use > >> list = (a .. b).inject([]){|accum, date| accum << date} > > > > I'm curious, is there a special reason for the use of inject here? > > Doesn't the following do the same? > > > > list = (a..b).to_a > > > >> -a > > > > Robin > > > > Yes, they are exactly the same. It's just that inject is a force of > nature. It consumes your Enumerables, and leaves a trail of > devastation in its wake. You can't escape it. You must accept your > fate as a slave to inject. Latterly though I've found respite from my own inject addiction thanks to a couple of quick benchmarks: a = (0...1000) # => 0...1000 Benchmark.bm { |x| x.report { 100.times { a.to_a } } } user system total real 0.060000 0.000000 0.060000 ( 0.075679) # => true Benchmark.bm { |x| x.report { 100.times { a.inject([]) { |a,e| a << e } } } } user system total real 0.300000 0.000000 0.300000 ( 0.431041) # => true -- Ross Bamford - rosco@roscopeco.REMOVE.co.uk