From: William James Date: 2006-03-01T17:28:36+09:00 Subject: Re: Your Favorite One Liner James Edward Gray II wrote: > On Feb 28, 2006, at 7:59 PM, Daniel Nugent wrote: > > > Give out your favorite one liner, what it does, and when you use it. > > This was originally posted by Erik Terpstra, with some editing from > others to improve it: > > >> str = 'This is a test of the emergency broadcasting services' > => "This is a test of the emergency broadcasting services" > >> str.scan(/(.{1,10}|\S{11,})(?:\s+|$)/) # poor man's word wrap > => [["This is a"], ["test of"], ["the"], ["emergency"], > ["broadcasting"], ["services"]] This doesn't properly handle whitespace between words. str = 'This is a test of the emergency broadcasting services here' p str.scan(/(.{1,10}|\S{11,})(?:\s+|$)/) ---> [["This is a "], ["test of"], ["the"], ["emergency"], ["broadcasting"], ["services"], ["here"]] Furthermore, \S{11,} can be \S+ str = 'This is a test of the emergency broadcasting services here' p str.scan(/(.{1,9}\S|\S+)(?:\s+|$)/) ---> [["This is a"], ["test of"], ["the"], ["emergency"], ["broadcasting"], ["services"], ["here"]]