Hello --

On Mon, 19 Nov 2001, Martin Kahlert wrote:

> Hi!
> I want to do something like that:
>
> a = Array.new(1000000, 1)
> b = Array.new(1000000, 2)
> c = Array.new(1000000)
>
> for i in 0 ... a.length
>    c[i] = a[i] + b[i]
> end
>
> This looks a bit too C-ish for me.
>
> Should i use
> (0 ... a.length).each {|i| c[i] = a[i] + b[i]}
> or
> a.length.times {|i| c[i] = a[i] + b[i]}
> or
> a.each_index {|i| c[i] = a[i] + b[i]}
>
> Which is the 'ruby way' of doing that?

Without having done benchmarks, I like:

  len = 1000000
  a = Array.new(len, 1)
  b = Array.new(len, 2)
  c = (0...len).map {|i| a[i] + b[i]}

though what I'd really like would be:

  c = a.map_with_indices {|e,i| e + b[i]}

but we don't have that.  (Well, I have it, but Ruby doesn't :-)


David

-- 
David Alan Black
home: dblack / candle.superlink.net
work: blackdav / shu.edu
Web:  http://pirate.shu.edu/~blackdav