From: nobu.nokada@... Date: 2002-12-14T16:02:07+09:00 Subject: Re: [RCR] Global Regexp Match Mechanism (//g) Hi, At Sat, 14 Dec 2002 14:27:22 +0900, Austin Ziegler wrote: > >> Also, if a MatchData is passed as str, it should implicitly > >> call MatchData#end for pos unless pos is non-nil. IMO, being > >> explicit about doing a //g-type match is a Good Thing. > > MatchData as String? Well, in such case I'd expect matching > > to $&(equals $~.to_s). > > Actually, I was thinking something like: > > def gmatch(str, pos = nil) > if str.class == MatchData > if pos.nil? > match(str.string, str.end(0)) > else > match(str.string, pos) > end > else > match(str, pos) > end > end > > I understand that checking the class is considered a bad thing, so > this should be viewed as a model, only. I worry if the behavior may surprise/confuse users. I got new idea, a new method like this. class MatchData def next_match(re) re.match(self.string, self.end(0)) end end md = /\A/.match(foostr) while md = md.next_match(/\Gfoo/) puts md.to_s end But MatchData#sring returns frozen copy, and the name never look good. Rather, StringScanner will be better. -- Nobu Nakada