William James <w_a_x_man / yahoo.com> wrote:
> =begin
> 
> Unlike Gawk and Mawk, Ruby won't accept a regular expression as a
> record-separator. Let's fix that. The substring matched by the
> record-separator is automatically removed from the record, but it
> can be obtained by RecSep#terminator.
> 
> Typical usage:
> 
> File.open("stuff.txt"){|handle|
>  reader = RecSep.new( handle, /^\d+\.\n/ )
>  reader.each {|x| p x }
> }

I'd prefer something integrated with IO, e.g.

File.open("foo") {|io| io.each_chunk(/:/) {|ch| p ch}}

module RegularIOChunks
  def each_chunk(rx, read_buffer = 1024)
    buff = ""
    loop do
      until ( match = ( rx.match( buff ) ) )
        part = read(read_buffer)

        if part.nil?
          yield buff
          return self
        end

        buff << part
      end

      yield match.pre_match
      buff = match.post_match
    end
  end
end

class IO
  include RegularIOChunks
end

Kind regards

    robert