Forum: Ruby on Rails Word wrap regexp question

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
912436ffe7c27e25658a3f5f556a8c96?d=identicon&s=25 Adam Fields (Guest)
on 2006-02-28 18:51
(Received via mailing list)
>From the rails helper, there's a text wrap method that does this (I've
removed the trailing .strip):

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

The way I read this, it's "replace every newline with two newlines
(and it's not clear to me either what this first part is supposed to
accomplish), then replace each instance of the longest string you can
under the line length with that string plus a newline".

This seems to work fine, except that if there are newlines in the
middle of the string, they get stripped.

So, a paragraph of text that was:

"Testing Testing Testing Testing Testing Testing Testing Testing Testing
Testing Testing Testing Testing Testing Testing Testing Testing Testing
Testing Testing Testing Testing Testing Testing.

Testing Testing Testing Testing Testing Testing Testing Testing Testing
Testing Testing Testing Testing Testing Testing Testing Testing Testing
Testing Testing Testing Testing Testing Testing Testing Testing Testing
Testing."

Becomes something like:

"Testing Testing Testing Testing Testing Testing Testing Testing
Testing Testing Testing Testing Testing Testing Testing Testing
Testing Testing Testing Testing Testing Testing Testing Testing.
Testing Testing Testing Testing Testing Testing Testing Testing
Testing Testing Testing Testing Testing Testing Testing Testing
Testing Testing Testing Testing Testing Testing Testing Testing
Testing Testing Testing Testing."

Why is the middle newline being removed?

--
				- Adam

** Expert Technical Project and Business Management
**** System Performance Analysis and Architecture
****** [ http://www.everylastounce.com ]

[ http://www.aquick.org/blog ] ............ Blog
[ http://www.adamfields.com/resume.html ].. Experience
[ http://www.flickr.com/photos/fields ] ... Photos
[ http://www.aquicki.com/wiki ].............Wiki
[ http://del.icio.us/fields ] ............. Links
This topic is locked and can not be replied to.