From: ahoward Date: 2003-05-30T07:25:30+09:00 Subject: Re: Iteration - last detection On Fri, 30 May 2003 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 this won't work for *anything* Enumerable though - though it works for Array and Hash. "The Enumerable mixin provides collection classes with several traversal and searching methods, and with the ability to sort. The class must provide a method each, which yields successive members of the collection. If Enumerable#max , #min, or #sort is used, the objects in the collection must also implement a meaningful <=> operator, as these methods rely on an ordering between members of the collection." so you may or may *not* have a size method. > (Hmmm... could this be the non-superfluous use of Enumerable#each_with_index > (as opposed to just Array#ewi) that I've been looking for for 2.5 years? :-) you don't like it? i use it all the time! ;-) -a -- ==================================== | Ara Howard | NOAA Forecast Systems Laboratory | Information and Technology Services | Data Systems Group | R/FST 325 Broadway | Boulder, CO 80305-3328 | Email: ara.t.howard@fsl.noaa.gov | Phone: 303-497-7238 | Fax: 303-497-7259 | ~ > ruby -e 'p % ^) .intern' ====================================