Iteration the ruby way

Lähettäjä: "William James" <w_a_x_man@yahoo.com>
Aihe: Re: iteration the ruby way

E S wrote:

> ary[0..-1].each {|e| do_something(e) }
> do_something_else(ary.last)

Shouldn't that be

ary[0...-1].each {|e| do_something(e) }
do_something_else(ary.last)

or

ary[0..-2].each {|e| do_something(e) }
do_something_else(ary.last)

?

Yes, or indeed, it was actually supposed to be ary[0...-1] :slight_smile:
In either case, as expected, the #inject-solution was superior.

E

Hi --

"William James" <w_a_x_man@yahoo.com>
Aihe: Re: iteration the ruby way

E S wrote:

  ary[0..-1].each {|e| do_something(e) }
  do_something_else(ary.last)

Shouldn't that be

ary[0...-1].each {|e| do_something(e) }
do_something_else(ary.last)

or

ary[0..-2].each {|e| do_something(e) }
do_something_else(ary.last)

?

Yes, or indeed, it was actually supposed to be ary[0...-1] :slight_smile:

(I think that's what William just said :slight_smile:

In either case, as expected, the #inject-solution was superior.

It's very interesting and could be useful, but where possible I'd tend
to prefer the ary[0...-1] technique, or the #each_[with]_index one.
They're shorter and, to me, somewhat more transparent. But I'm
certainly not complaining about the development of the #inject one,
which is something I would probably never have come up with.

David

···

On Sat, 5 Feb 2005, E S wrote:

--
David A. Black
dblack@wobblini.net