From: Robert Klemme Date: 2007-10-27T01:55:01+09:00 Subject: Re: Skip the first invocation e.g. skip_first { foo } On 26.10.2007 17:50, mortee wrote: > Robert Klemme wrote: >> 2007/10/26, mortee : >>> The bad news is that I guess from inside the block passed to the >>> iterator, you have no way (at least no nice way) to keep track of the >>> iteration count, and make decisions based on it. You either have to call >>> each_with_index, and make use of the passed index, but this won't work >>> with other iterators. >> It does, if you use Enumerator: >> >> 17:26:33 ~$ irb -r enumerator >> irb(main):001:0> %w{foo bar baz}.to_enum(:each_with_index). >> irb(main):002:0* map {|e,i| i % 2 == 0 ? e : :empty} >> => ["foo", :empty, "baz"] >> irb(main):003:0> > > Thanks, this is nice - I haven't known it existed. However, > unfortunately it's less "elegant" than simply using the iterator itself, > and enclosing parts of the loop body in skip_first{...} Obviously tastes differ. :-) Personally I find the effort you have to invest to get a correct working and failure safe version of skip_first extremely inelegant for a problem which has a much simpler solution. Kind regards robert