From: Florian Frank Date: 2003-05-30T07:20:15+09:00 Subject: Re: Iteration - last detection On 2003-05-30 06:46:42 +0900, dblack@superlink.net wrote: > Let Ruby do more of the work for you :-) > > module Enumerable > def each_with_last > each_with_index {|e,i| yield(e, i == size-1)} > end > end Hmmm... Too much work isn't good either. If Enumerable#size is defined in terms of #each your iteration is in O(n ** 2). This would fix that problem: module Enumerable def each_with_last size_minus_1 = size - 1 each_with_index { |e,i| yield(e, i == size_minus_1) } end end -- Faith is that quality which enables us to believe what we know to be untrue.