From: "David A. Black" Date: 2005-02-07T05:59:52+09:00 Subject: Re: iteration the ruby way --927295978-213545966-1107723579=:13178 Content-Type: MULTIPART/MIXED; BOUNDARY="927295978-213545966-1107723579=:13178" This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. --927295978-213545966-1107723579=:13178 Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE Hi -- On Mon, 7 Feb 2005, E S wrote: >> "Robert Klemme" >> Aihe: Re: iteration the ruby way >> >> >> "E S" schrieb im Newsbeitrag >> news:20050206143902.PIMQ15813.fep31-app.kolumbus.fi@mta.imail.kolumbus.f= i... >>>> L=E4hett=E4j=E4: Kaspar Schiess >>>> Aihe: Re: iteration the ruby way >>>> (In response to news:1107566961.943586.81760@g14g2000cwa.googlegroups.= com >>> by William James) >>>>> 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) >>>>> >>>>> ? >>>> >>>> I would prefer this solution for its clarity of expression. >>> >>> Me too :) I just think the #inject won on the neat-o factor, >>> particularly since it seemed adequate for the OP. >> >> Partly yes and partly no: apart from the cute factor, there is a real >> advantage of the inject solutions show: they work for *all* Enumerables >> while the array indexing works only for arrays. So #inject is more gene= ral. >> Also, Array#[] creates a new array instance along the way which is a bit >> inefficient. But I do agree that the indexing might be more readable. > > I think assuming an Array is valid in the described scenario (which > is the last element in a Hash, after all?) If you goal was to create a string representation of the hash, with some kind of marker between each key/value pair -- but not after the last one -- then the last-ness of the last element would be important in the sense that you'd treat it differently (whatever it was, and whether or not it remained the same from one run to another). David --=20 David A. Black dblack@wobblini.net --927295978-213545966-1107723579=:13178-- --927295978-213545966-1107723579=:13178--