James Edward Gray II 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"]]
···
On Feb 28, 2006, at 7:59 PM, Daniel Nugent wrote: