From: Holger Mack Date: 2008-09-21T06:45:17+09:00 Subject: Re: [QUIZ] One-Liners Mashup (#177 again) ------=_Part_329_25900597.1221947594376 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline wrap text with single regexp and strict maximum length text.gsub(/(.{1,80})\s|(\w{80})/, "\\1\\2\n") Last one for now, and no followup (we had one for this quiz already) Regards Holger 2008/9/20 James Coglan > > > > New puzzle: Provide a one-liner that can wrap a body of text at a > > requested maximum length. > > > > A first guess... this destroys all original whitespace in the string, > currently trying to fix that. > > class String > def line_wrap(n = 80) > split(/\s+/).inject{ |s,w| s + ("#{s.split("\n").last} #{w}".size > n ? > "\n" : " ") + w } > end > end > ------=_Part_329_25900597.1221947594376--