From: Pit Capitain Date: 2004-08-11T06:20:30+09:00 Subject: Re: FirstEachLast, an extension to the Enumerable module. Robert Klemme wrote: >The index clearly belongs to the *iteration* and not the elements. Then you should create a new object for the iteration state. How about this: module Enumerable class State include Enumerable def initialize enum @enum = enum @index = -1 end def each @enum.each do |e| if @last.nil? @last = false else @index += 1 yield @next end @next = e end unless @last.nil? @last = true @index += 1 yield @next end end def first? @index == 0 end def index @index end def last? @last end end def state State.new self end end Now you can get an EnumState object from a normal enumerable and iterate over the EnumState object: state = %w"does it work?".state state.each do |e| puts "%-5s %-5s %i %s" % [ e, state.first?, state.index, state.last? ] end => does true 0 false it false 1 false work? false 2 true Regards, Pit