William James
3/1/2006 9:26:00 AM
William James wrote:
> 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"]]
One character shorter and eliminates nesting of arrays:
str =
'This is a test of the emergency broadcasting servicings I asseverate'
p str.scan(/\S.{0,8}\S(?=\s|$)|\S+/)
--->
["This is a", "test of", "the", "emergency", "broadcasting",
"servicings", "I", "asseverate"]