Robert Klemme wrote: > 2007/10/26, mortee <mortee.lists / kavemalna.hu>: >> 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{...} mortee