Forum: Ruby on Rails Change Proposal for "String#word_wrap"

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
A131b672fdbd2a58dce12031ad78b121?d=identicon&s=25 Wolfgang Nádasi-Donner (wonado)
on 2007-02-04 15:41

I posted this first in comp.lang.ruby, but I think, this here is the
better place. I cannot tell more details, because I'm not a Rails user.
I recognized this method by a hint in the german Ruby forum.

The Method "String#word_wrap" is defined as

def word_wrap(text, line_width = 80)
   text.gsub(/\n/, "\n\n").gsub(/(.{1,#{line_width}})(\s+|$)/,

The sense of the part 'gsub(/\n/, "\n\n")' is not visible for me,
newlines will be removed in the next step by '\s+'.

I made some tests (Ruby, not Ruby on Rails) and couldn't find any
difference to
the following definition:

def word_wrap(text, line_width = 80)
   text.gsub(/(.{1,#{line_width}})(\s+|$)/, "\\1\n").strip

Wolfgang Nádasi-Donner
This topic is locked and can not be replied to.