From: Robert Klemme Date: 2007-04-03T20:10:07+09:00 Subject: Re: Last iteration condition On 03.04.2007 10:24, Gary Wright wrote: > > On Apr 3, 2007, at 3:50 AM, Brian Candler wrote: > >> On Tue, Apr 03, 2007 at 03:31:09PM +0900, Gary Wright wrote: >>> Building off of Brian's suggestion in that thread, here >>> is a way to provide a 'countdown' as the end of the iteration >>> approaches. Default is to only flag the last item but you >>> can ask for any number of items to be flagged. >> >> Interesting. Maybe it would be cleaner to return nil for all >> non-countdown >> items, and then n-1, n-2 ... 0 as the flag (or n, n-1 ... 1). e.g. > > I liked the idea of the flag being true for all but the last item. > It just makes boolean expressions nicer for that special case even > when you are counting down. No need for complex new code, this can be done with #inject very simply: irb(main):005:0> print "back ", %w{aaa bbb ccc ddd}.inject {|x,y| print "front ", x, "\n"; y}, "\n" front aaa front bbb front ccc back ddd => nil The trick is to use the form without arguments. Kind regards robert