On Sun, 2004-10-10 at 12:25, trans. (T. Onoma) wrote:
> Notice the three lines of duplication. Any ideas on DRYing this up?
>     
>     def each
>       seed = @start
>       if @step
>         @step.times do
>           yield(seed)
>           @skip.times { seed = seed.succ }          
>           break if @halt.call(seed) if @halt
>         end
>       else
>         loop do
>           yield(seed)
>           @skip.times { seed = seed.succ }
>           break if @halt.call(seed) if @halt
>         end
>       end
>     end
> 

How about:

    Forever = Object.new
    def Forever.times
        loop {yield}
        end

    def each
        seed = @start
        (@step || Forever).times do
            yield(seed)
            @skip.times { seed = seed.succ }          
            break if @halt.call(seed) if @halt
          end
     end


-- Markus