# Word Wrapping at n characters

**URL:** <https://www.ruby-forum.com/t/word-wrapping-at-n-characters/175392>\
**Category:** Ruby\
**Created:** [September 12, 2009, 2:19pm UTC](https://www.ruby-forum.com/t/word-wrapping-at-n-characters/175392 "2009-09-12T14:19:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![quik77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/dec6dc/32.png) [@quik77](https://www.ruby-forum.com/u/quik77)\
**Post date:** [September 12, 2009, 2:19pm UTC](https://www.ruby-forum.com/t/word-wrapping-at-n-characters/175392/1 "2009-09-12T14:19:22Z")

</div>

Right now I basically use this code for word wrapping:

def word\_wrap(line\_width = 80)  
self.gsub(/(.{1,#{line\_width}})(\s+|$)/, “\1\n”)  
end

Is anyone using a better solution? This method should be  
robust enough to do “proper” line wrapping of really  
long strings - like strings long enough to fill  
a big book.

---

<div class="post-metadata">

**Author:** ![quik77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/dec6dc/32.png) [@quik77](https://www.ruby-forum.com/u/quik77)\
**Post date:** [September 12, 2009, 3:54pm UTC](https://www.ruby-forum.com/t/word-wrapping-at-n-characters/175392/2 "2009-09-12T15:54:00Z")

</div>

Here is an alternative with analysis and comments:

[http://blog.macromates.com/2006/wrapping-text-with-regular-expressions/](http://blog.macromates.com/2006/wrapping-text-with-regular-expressions/)

The code looks like this:

def wrap\_text(txt, col = 80)  
txt.gsub(/(.{1,#{col}})( +|$\n?)|(.{1,#{col}})/,  
“\1\3\n”)  
end

regards,  
Tor Erik  
[http://tel.jklm.no/](http://tel.jklm.no/)

---

<div class="post-metadata">

**Author:** ![quik77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/dec6dc/32.png) [@quik77](https://www.ruby-forum.com/u/quik77)\
**Post date:** [September 12, 2009, 8:49pm UTC](https://www.ruby-forum.com/t/word-wrapping-at-n-characters/175392/3 "2009-09-12T20:49:58Z")

</div>

7stud – wrote:

> str =\<\<EOS  
> hello hello hello hello hello hello hello hello hello hello hello hello  
> hello hello  
> goodbye goodbye goodbye goodbye  
> EOS

Note all the hello’s were on one line.

> –output:–  
> hello hello hello hello hello hello hello hello hello hello hello hello  
> hello  
> hello  
> goodbye goodbye goodbye goodbye

The output is really:

line #1: hello’s  
line #2: one hello  
line #3: goodbye’s

---

<div class="post-metadata">

**Author:** ![quik77](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/q/dec6dc/32.png) [@quik77](https://www.ruby-forum.com/u/quik77)\
**Post date:** [September 12, 2009, 8:47pm UTC](https://www.ruby-forum.com/t/word-wrapping-at-n-characters/175392/4 "2009-09-12T20:47:05Z")

</div>

Marc H. wrote:

> Right now I basically use this code for word wrapping:
> 
> def word\_wrap(line\_width = 80)  
> self.gsub(/(.{1,#{line\_width}})(\s+|$)/, “\1\n”)  
> end
> 
> Is anyone using a better solution? This method should be  
> robust enough to do “proper” line wrapping of really  
> long strings - like strings long enough to fill  
> a big book.

str =\<\<EOS  
hello hello hello hello hello hello hello hello hello hello hello hello  
hello hello  
goodbye goodbye goodbye goodbye  
EOS

class String  
def word\_wrap(line\_width = 80)  
self.gsub(/(.{1,#{line\_width}})(\s+|$)/, “\1\n”)  
end  
end

puts str.word\_wrap

–output:–  
hello hello hello hello hello hello hello hello hello hello hello hello  
hello  
hello  
goodbye goodbye goodbye goodbye

Is that what you want?
