Robert Klemme wrote: > 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 This would *not* be easy to implement. Consider backtracking (do we put it back in the stream?) and greediness (how much do we read?). Unless you want to forbid greedy regular expressions and ignore backtracking (not to mention certain switches), this gets real ugly, real quick. This has come up wrt Perl as well on p5p. Take a look here for one thread in midstream: http://www.nntp.perl.org/group/perl.perl5.porters/64830 Rumor has it that setting $/ to a regex will be legal in Perl 6, but I think there will be several restrictions. Regards, Dan