On Feb 2, 9:44 am, Robert Klemme <shortcut... / googlemail.com> wrote:
> On 02.02.2007 17:28, Phrogz wrote:
> > str.gsub( /^(.{#{str.length/2},}?)\s+(.+)/ ){ "#{$1}\n#{$2}" }
>
> You're making length/2 the minimum for matching.  I believe that should
> rather be the max:
>
> # untested
> str.sub( /\A(.{1,#{str.length/2}})\s+(.+)/ ){ "#{$1}\n#{$2}" }

I suppose it depends on whether you want the first line to be longer
or shorter than the 2nd line. In my mind, it looks better longer. The
{x,} range does make it the minimum, but the non-greedy quantifier
ensures that it breaks as soon as possible after starting the word
that you're in the middle of.

> Also, #sub seems sufficient.

Good point. I don't think I have ever used #sub, so it's never at the
forefront of my mind.