Daniel Harple
3/13/2006 2:28:00 PM
On Mar 13, 2006, at 1:56 PM, Jeff Coleman wrote:
> For a text adventure program I'm working on I needed a method to word
> wrap a long string based on spaces or non-word characters so I came up
> with this. I just thought I'd post it to see if there are any
> thoughts
> or if there might be a better way to do it.
This one is taken from facets[1]. I use it to wrap lines in TextMate.
def word_wrap(text, col_width=80)
text.gsub!( /(\S{#{col_width}})(?=\S)/, '\1 ' )
text.gsub!( /(.{1,#{col_width}})(?:\s+|$)/, "\\1\n" )
text
end
[1] gem install facets
-- Daniel