From: John Carter Date: 2004-08-11T07:00:27+09:00 Subject: Re: FirstEachLast, an extension to the Enumerable module. On Tue, 10 Aug 2004, Michael Neumann wrote: > Why not something like this (each_with_first_last does not exist!): > > blah.each_with_first_last {|element, pos| > case pos > when :inside # or middle > ... > when :first > ... > when :last > ... > end > } I like the cleaner syntax. > I know, you could also use each_with_index instead (this one exists!): > > blah.each_with_index {|element, inx| > if inx == 0 > # first > elsif inx < blah.size > # inside > else > # last > end > } > > But that might not work for all Enumerable's. Nope, that is order N^2 for enumerables without a O(1) implementation of size. Simply won't do for me. > Or maybe, an extended each_with_index2?: Yip, could hack the .c code for each_with_index of enum.c blah.first_each_last do |e,pos| case pos when :first .... when :inner .... when :last ...... when :empty ..... end end John Carter Phone : (64)(3) 358 6639 Tait Electronics Fax : (64)(3) 359 4632 PO Box 1645 Christchurch Email : john.carter@tait.co.nz New Zealand The universe is absolutely plastered with the dashed lines exactly one space long.