From: Ara.T.Howard@... Date: 2004-09-21T03:14:39+09:00 Subject: Re: Array#index block and rdetect On Tue, 21 Sep 2004, trans. (T. Onoma) wrote: > One begins to wonder if maybe blocks might have "underwritten" parameters. Not > sure how it might work, but basically there would be special global > variables. Pseudo-example: > > ["A", "B", "C"].each {|e| p [e, $count, $begin, $end] } > > ["A", 0, 0, 2] > ["B", 1, 0, 2] > ["C", 2, 0, 2] good idea but evil impl! ;-) perhaps a block iterator proxy object: class BlockIteratorProxy %w( count begin end proxy ).each{|a| attr a, true} def intialize proxy @proxy = nil end def method_missing(*a,&b);proxy.send(*a,&b);end end eg. usage class Container include Enumerable def each bi = BlockIterator.new @objects.each do |obj| bi.proxy = obj yield bi end end end a module and dynamic object extension could also be used: module BlockIteratorProxyMethods ... end class Container def each @objects.each do |obj| obj.extend BlockIteratorProxyMethods yield obj end end end you get the idea though... another idea would be for the blocks to provide a LoopContext object - sort of the state bit torn out from an iterator... cheers. -a -- =============================================================================== | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov | PHONE :: 303.497.6469 | A flower falls, even though we love it; | and a weed grows, even though we do not love it. | --Dogen ===============================================================================