From: Gary Wright Date: 2007-04-03T13:17:43+09:00 Subject: Re: Last iteration condition On Apr 2, 2007, at 11:00 PM, Mike wrote: > Hi, > > How could I know that current iteration is the last in the series? > For example: > > [1, 2, 3, 4, 5, 3, 4].each do |element| > print element.to_s > print ',' if !LAST > end Here are a couple of ideas: require 'generator' require 'enumerator' a = [1,2,3,4] g = Generator.new(a) while g.next? item = g.next if g.next? puts "#{item} is not the last item" else puts "#{item} is the last item" end end a = [5,6,7,8] a.push(final = Object.new) Enumerable::Enumerator.new(a).each_cons(2) do |a, b| if b == final puts "#{a} is the last item" else puts "#{a} is not the last item" end end # Gary Wright