From: Robert Klemme Date: 2007-07-20T21:09:32+09:00 Subject: Re: Is there a replacement for sub? 2007/7/20, Martin DeMello : > On 7/20/07, Robert Dober wrote: > > > > I was reading this whole thread and I kind of think to be dreaming, I > > must have missed something obvious!!! > > Anyway maybe this is was OP wants, well I think it is ;) > > > > 529/29 > irb > > irb(main):001:0> a='a b c d e f' > > => "a b c d e f" > > irb(main):002:0> a.gsub!(" ","") > > Nope, he wants a method that only replaces the first n occurrences of > the pattern. gsub will not do this - you need to run sub in a loop. There is a different solution that does not need a loop (see one of my previous posts). > (This is where perl's "continue matching where you left off" would > have been a nice optimisation) Well, you can always use the block form with an additional counter irb(main):012:0> count=0 => 0 irb(main):013:0> a.gsub(/ /) {count+=1; count<3 ? "" : " "} => "abc d e f" But I guess the other solution is more efficient. Kind regards robert