# Problems with making wordwrap

**URL:** <https://www.ruby-forum.com/t/problems-with-making-wordwrap/48427>\
**Category:** Ruby\
**Created:** [November 20, 2005, 7:09am UTC](https://www.ruby-forum.com/t/problems-with-making-wordwrap/48427 "2005-11-20T07:09:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![l.d.u.n.c.a.l.f.e](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5f9b8f/32.png) [@l.d.u.n.c.a.l.f.e](https://www.ruby-forum.com/u/l.d.u.n.c.a.l.f.e)\
**Post date:** [November 20, 2005, 7:09am UTC](https://www.ruby-forum.com/t/problems-with-making-wordwrap/48427/1 "2005-11-20T07:09:55Z")

</div>

hi,

i’ve found a great bit of code from

[http://dev.rubyonrails.org/ticket/1449](http://dev.rubyonrails.org/ticket/1449)

that takes some text and makes it wrap at 80 lines. the only issue is  
that  
it takes multiple line breaks (like paragraphs), and makes them into  
single  
line breaks.

this is the code:

text.gsub( /\n/, “\n\n” ).gsub( /(.{1,80})(\s+|$)/, “\1\n”)

i’ve tinkered with it for a while, but i can’t work out how to not have  
it  
eat lines breaks so hungrily.

thanks for any help  
luke

---

<div class="post-metadata">

**Author:** ![l.d.u.n.c.a.l.f.e](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5f9b8f/32.png) [@l.d.u.n.c.a.l.f.e](https://www.ruby-forum.com/u/l.d.u.n.c.a.l.f.e)\
**Post date:** [November 20, 2005, 8:34am UTC](https://www.ruby-forum.com/t/problems-with-making-wordwrap/48427/2 "2005-11-20T08:34:14Z")

</div>

luke schrieb:

> text.gsub( /\n/, “\n\n” ).gsub( /(.{1,80})(\s+|$)/, “\1\n”)
> 
> i’ve tinkered with it for a while, but i can’t work out how to not have it  
> eat lines breaks so hungrily.

Hi Luke,

can you tell us how you want to treat whitespace in the given text? You  
talked about newlines, but what should be the result in the following  
cases:

“abcde\n c”.wrap(5) # =\> “abcde\nc” or “abcde\n c”  
“a e\na”.wrap(5) # =\> “a e\na” or “a e a”

Regards,  
Pit

---

<div class="post-metadata">

**Author:** ![l.d.u.n.c.a.l.f.e](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5f9b8f/32.png) [@l.d.u.n.c.a.l.f.e](https://www.ruby-forum.com/u/l.d.u.n.c.a.l.f.e)\
**Post date:** [November 20, 2005, 9:02am UTC](https://www.ruby-forum.com/t/problems-with-making-wordwrap/48427/3 "2005-11-20T09:02:22Z")

</div>

On Nov 19, 2005, at 10:07 PM, luke wrote:

> line breaks.  
> I use Text::Format for this:

def wrap(text)  
formatter = Text::Format.new  
formatter.columns = 72 # 70 + '\> ’ = 72  
formatter.format\_style = Text::Format::LEFT\_ALIGN  
formatter.first\_indent = 0

formatted = []

text.split(/\r?\n\r?\n/).each do |chunk|  
formatted \<\< formatter.paragraphs(chunk)  
end

return formatted.join(“\n”)  
end

[http://www.halostatue.ca/ruby/Text\_\_Format.html](http://www.halostatue.ca/ruby/Text__Format.html)

---

<div class="post-metadata">

**Author:** ![l.d.u.n.c.a.l.f.e](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5f9b8f/32.png) [@l.d.u.n.c.a.l.f.e](https://www.ruby-forum.com/u/l.d.u.n.c.a.l.f.e)\
**Post date:** [November 20, 2005, 10:18am UTC](https://www.ruby-forum.com/t/problems-with-making-wordwrap/48427/4 "2005-11-20T10:18:44Z")

</div>

> > i’ve found a great bit of code from
> > 
> > [http://dev.rubyonrails.org/ticket/1449](http://dev.rubyonrails.org/ticket/1449)
> > 
> > that takes some text and makes it wrap at 80 lines. the only issue is  
> > that  
> > it takes multiple line breaks (like paragraphs), and makes them into  
> > single  
> > line breaks.
> > 
> > this is the code:
> > 
> > text.gsub( /\n/, “\n\n” ).gsub( /(.{1,80})(\s+|$)/, “\1\n”)
> > 
> > i’ve tinkered with it for a while, but i can’t work out how to not have  
> > it  
> > eat lines breaks so hungrily.
> 
> Hi Luke,
> 
> can you tell us how you want to treat whitespace in the given text? You  
> talked about newlines, but what should be the result in the following  
> cases:
> 
> “abcde\n c”.wrap(5) # =\> “abcde\nc” or “abcde\n c”  
> “a e\na”.wrap(5) # =\> “a e\na” or “a e a”
> 
> Regards,  
> Pit

hi pit,

thanks for your help.

ideally how i would like it to behave given your examples, would be  
like:

“abcde\n c”.wrap(5) # =\> “abcde\nc”  
“a e\na”.wrap(5) # =\> “a e\na”

does that make sense? so text would be flush with the left margin with  
no  
white space at the beginning of new lines. but whitespace within a line  
should remain intact.

thanks

luke

---

<div class="post-metadata">

**Author:** ![l.d.u.n.c.a.l.f.e](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5f9b8f/32.png) [@l.d.u.n.c.a.l.f.e](https://www.ruby-forum.com/u/l.d.u.n.c.a.l.f.e)\
**Post date:** [November 20, 2005, 1:51pm UTC](https://www.ruby-forum.com/t/problems-with-making-wordwrap/48427/5 "2005-11-20T13:51:55Z")

</div>

luke schrieb:

> ideally how i would like it to behave given your examples, would be like:
> 
> “abcde\n c”.wrap(5) # =\> “abcde\nc”  
> “a e\na”.wrap(5) # =\> “a e\na”
> 
> does that make sense? so text would be flush with the left margin with no  
> white space at the beginning of new lines. but whitespace within a line  
> should remain intact.

Of course it makes sense. Here’s one version:

class String  
def wrap n  
gsub(  
/  
\b # word boundary  
[\t\r\f]\* # whitespace (no newline)  
\n # newline  
[\t\r\f]\* # whitespace (no newline)  
\b # word boundary  
/x,  
" " # replaced by space  
).gsub(  
/  
(.{1,#{n}}) # upto n characters  
( # followed by either:  
\n # exactly one newline  
|\s+ # or other whitespace characters  
)  
/x,  
“\1\n” # insert newline after first part  
)  
end  
end

I used extended regular expressions to show what they are doing. You can  
shorten them if you want.

The first gsub replaces newlines inside of paragraphs into spaces, but  
leaves newlines between paragraphs unchanged. The second gsub is mostly  
the original one, but it consumes at most one newline character  
(replacing it with itself). This has the effect that newlines between  
paragraphs are preserved.

If you still have problems or questions, feel free to ask.

Regards,  
Pit

---

<div class="post-metadata">

**Author:** ![l.d.u.n.c.a.l.f.e](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5f9b8f/32.png) [@l.d.u.n.c.a.l.f.e](https://www.ruby-forum.com/u/l.d.u.n.c.a.l.f.e)\
**Post date:** [November 20, 2005, 11:56pm UTC](https://www.ruby-forum.com/t/problems-with-making-wordwrap/48427/6 "2005-11-20T23:56:29Z")

</div>

luke (dot) wrote:

> this is the code:
> 
> text.gsub( /\n/, “\n\n” ).gsub( /(.{1,80})(\s+|$)/, “\1\n”)
> 
> i’ve tinkered with it for a while, but i can’t work out how to not have it  
> eat lines breaks so hungrily.
> 
> thanks for any help  
> luke

text.gsub( /(.{1,39}[^\s])([\t]_\n[\t]_|[\t]+|$)/, “\1\n”)

---

<div class="post-metadata">

**Author:** ![l.d.u.n.c.a.l.f.e](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5f9b8f/32.png) [@l.d.u.n.c.a.l.f.e](https://www.ruby-forum.com/u/l.d.u.n.c.a.l.f.e)\
**Post date:** [November 20, 2005, 3:52pm UTC](https://www.ruby-forum.com/t/problems-with-making-wordwrap/48427/7 "2005-11-20T15:52:08Z")

</div>

On 11/20/05, Eric H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Nov 19, 2005, at 10:07 PM, luke wrote:  
> I use Text::Format for this:

If I remember correctly, portions or the whole of Text::Format is  
included with rails-core, too, but it may not be easily accessible.

-austin

---

<div class="post-metadata">

**Author:** ![l.d.u.n.c.a.l.f.e](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5f9b8f/32.png) [@l.d.u.n.c.a.l.f.e](https://www.ruby-forum.com/u/l.d.u.n.c.a.l.f.e)\
**Post date:** [November 21, 2005, 2:33am UTC](https://www.ruby-forum.com/t/problems-with-making-wordwrap/48427/8 "2005-11-21T02:33:29Z")

</div>

thanks pit,

works like a treat! nice idea of having it in the string class.

thanks a lot,  
luke

“Pit C.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in message  
news:removed\_email\_address@domain.invalid…

> luke schrieb:
> 
> > ideally how i would like it to behave given your examples, would be  
> > like:
> > 
> > “abcde\n c”.wrap(5) # =\> “abcde\nc”  
> > “a e\na”.wrap(5) # =\> “a e\na”
> > 
> > does that make sense? so text would be flush with the left margin with  
> > no
