From: dblack@... Date: 2003-05-30T07:58:35+09:00 Subject: Re: Iteration - last detection On Fri, 30 May 2003, ahoward wrote: > module Enumerable > def each_with_last(&block) > l = nil > each_with_index do |e, i| > block.call l, false if i > 0 > l = e > end > block.call l, true > end > end OK, as penance for clouding the issue with the mythical Enumerable#size, I feel morally obliged to write a real version :-) I'm not sure how horrible this is. Very, I suspect. But anyway, another possibility: module Enumerable def each_with_last n = -1 each { n += 1 } each_with_index {|e,i| yield(e, i == n)} end end It benchmarks a bit faster than yours, for arrays of size 10 to 10000, but not wildly so. David -- David Alan Black home: dblack@superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav