From: Robert Klemme Date: 2004-08-09T16:21:20+09:00 Subject: Re: FirstEachLast, an extension to the Enumerable module. "John Carter" schrieb im Newsbeitrag news:Pine.LNX.4.61.0408091724450.32554@parore.tait.co.nz... > I know somewhere is a collection of extensions to the enumerable module. > > Is the rubygarden page > http://www.rubygarden.org/ruby?StandardClassExtensions/Enumerable > the only one? > > Anyway, I have just added FirstEachLast to that. > > Use it like so... > > require 'FirstEachLast' > > def test_first_each_last(any_enumerable_object) > result = nil > count = > any_enumerable_object.first_each_last( proc {|first| > result = "First #{first}" > }, > proc {|i| > result += " then #{i}," > }, > proc {|last| > result += " and last of all #{last}" > }, > proc {|| result = 'Empty!'} > > puts count > puts result > end > > test_first_each_last( [1,2,3,4]) > test_first_each_last( []) > > Will result in the following output... > 4 > First 1 then 2, then 3, and last of all 4 > 0 > Empty! Nice output, but what do you need that for? robert