From: Gavin Kistner Date: 2005-09-15T09:37:07+09:00 Subject: Re: Quick and dirty word wrapping. --Apple-Mail-2-261051077 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed On Sep 14, 2005, at 4:04 PM, David Tran wrote: >> "Kroeger Simon (ext)" wrote: >> Hmm, nice, just one little thing: >> it 'eats' chars... > > Hmmm.... Here is my quick fix for him ... > > class String > def wrap_to( col_width ) > str = self.gsub( /(\S{#{col_width}})(?=\S)/, '\1 ' ) > str.scan(/(.{1,#{col_width}})(?:\s+|$)/).flatten.join( "\n" ) > end > end Very nice. My proprocessing didn't eat any characters, but it did allow for \S runs that were col_width+1 long, which the next step skipped over. David's lookahead does the trick nicely. --Apple-Mail-2-261051077--