>>>>> "C" == Carlos  <angus / quovadis.com.ar> writes:

C> class Object
C> 	def last?
C> 		Thread.current.last_iteration?
C> 	end
C> 	private :last?
C> end

   def last?
      # ...
   end

 is faster to write :-)

C> 				#{old_each}(*a) { |e|
C> 					Thread.current.instance_eval {
C> 						@__last_iteration__=false
C> 					}

 ruby has thread local variable, you can write it

          Thread.current['__last_iteration__'] = false

C> class Array; include EachWithLast end

C> a = [1,2,3,4,5].select {|e| last? }

 unfortunately it will not work with 1.8, Array don't use #each for #select
 in 1.8


Guy Decoux


svg% cat b.rb
#!./ruby
def last?
   Thread.current['__last__']
end

module EachWithLast
   def each:hook
      element = nil
      flag = false
      super do |e|
         Thread.current['__last__'] = false
         yield element if flag
         flag = true
         element = e
      end
      if flag
         Thread.current['__last__'] = true
         yield element
         Thread.current['__last__'] = false
      end
   end
end

class Array; include EachWithLast end

a = [1,2,3,4,5].each {|e| p "received #{e} #{last?}" }

svg% 

svg% b.rb 
"received 1 false"
"received 2 false"
"received 3 false"
"received 4 false"
"received 5 true"
svg%