Hi,

I know we had a similar thread in the past about first/last-element
detection.

In Smalltalk there's #do:separateBy: which basically works like
each+join combined, but in an imperative fashion.

Now I tried to implement:

  [1,2,3].separate_by { puts "-" }.each {|obj| 
    puts obj
  }

desired output:

  1
  -
  2
  -
  3

This is how I thought it would work:

  require 'generator'

  module Enumerable
    def separate_by(&block)
      last = size - 1
      Generator.new {|g|
        each_with_index {|obj, inx|
          g.yield obj
          block.call if inx < last
        }
      }
    end
  end

But the output is as follows:

  -
  1
  -
  2
  3

g.yield does not actually stop until an item is consumed. 

Any idea how to solve this?

Regards,

  Michael