# Word\_wrap method for String?

**URL:** <https://www.ruby-forum.com/t/word-wrap-method-for-string/80306>\
**Category:** Ruby\
**Created:** [March 13, 2006, 1:56pm UTC](https://www.ruby-forum.com/t/word-wrap-method-for-string/80306 "2006-03-13T13:56:04Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [March 13, 2006, 1:56pm UTC](https://www.ruby-forum.com/t/word-wrap-method-for-string/80306/1 "2006-03-13T13:56:04Z")

</div>

Hi all,

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.

class String  
def word\_wrap(width)  
source = self.dup  
original\_width = width  
while width \< source.length do  
last\_space = source.rindex( / |\W/, width )  
source.insert( last\_space, “\n” )  
source.gsub!(/\n \*/,"\n")  
width = last\_space + original\_width  
end  
source  
end  
end

Jeff C.man

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [March 13, 2006, 2:19pm UTC](https://www.ruby-forum.com/t/word-wrap-method-for-string/80306/2 "2006-03-13T14:19:04Z")

</div>

here’s a (partially modded) excerpt from a previous discussion on this  
topic:

width = 11  
str = ‘This is a test of the emergency broadcasting servicings I  
asseverate’  
p str.scan(/\S.{0,#{width}}\S(?=\s|$)|\S+/)

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [March 13, 2006, 2:06pm UTC](https://www.ruby-forum.com/t/word-wrap-method-for-string/80306/3 "2006-03-13T14:06:46Z")

</div>

> 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.

I wrote one last week, actually

def wrap(wrap\_len=78)  
start\_pos = wrap\_len  
while start\_pos \< @fact.length  
sp = @fact.rindex(’ ', start\_pos)  
@fact.insert(sp, ‘|’)  
start\_pos = sp + wrap\_len + 1  
end  
@fact.gsub!(/|[\s]/, “\n”)  
end

I’m far from an expert, so I don’t know which method may be better 🙂

-M

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [March 13, 2006, 2:23pm UTC](https://www.ruby-forum.com/t/word-wrap-method-for-string/80306/4 "2006-03-13T14:23:38Z")

</div>

Lou V. wrote:

> here’s a (partially modded) excerpt from a previous discussion on this  
> topic:
> 
> width = 11  
> str = ‘This is a test of the emergency broadcasting servicings I  
> asseverate’  
> p str.scan(/\S.{0,#{width}}\S(?=\s|$)|\S+/)

When I was writing that I just KNEW there must be some one-line solution  
involving regular expressions!

Although I don’t quite follow the exact structure of this one, it  
definitely works…

Jeff

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [March 13, 2006, 2:33pm UTC](https://www.ruby-forum.com/t/word-wrap-method-for-string/80306/5 "2006-03-13T14:33:48Z")

</div>

Mike 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.
> 
> I wrote one last week, actually
> 
> def wrap(wrap\_len=78)  
> start\_pos = wrap\_len  
> while start\_pos \< @fact.length  
> sp = @fact.rindex(’ ', start\_pos)  
> @fact.insert(sp, ‘|’)  
> start\_pos = sp + wrap\_len + 1  
> end  
> @fact.gsub!(/|[\s]/, “\n”)  
> end
> 
> I’m far from an expert, so I don’t know which method may be better 🙂
> 
> -M

class String  
# Wrap string by the given length, and join it with the given  
character.  
# The method doesn’t distinguish between words, it will only work based  
on  
# the length. The method will also strip and whitespace.  
#  
def wrap(length = 80, character = $/)  
scan(/.{#{length}}|.+/).map { |x| x.strip }.join(character)  
end  
end

puts ‘Hello, World! I am quite very long :)’.wrap(15, “  
\n”)

# Output:

# 

# Hello, World! I

# am quite very

# long 🙂

Have fun =)

- Matt

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [March 13, 2006, 2:53pm UTC](https://www.ruby-forum.com/t/word-wrap-method-for-string/80306/6 "2006-03-13T14:53:38Z")

</div>

Lou V. wrote:

> everthing old is new again.  
> you might also want to compare speeds. Yours might be faster.  
> -lv

It’s my first time using the profiler, but if I’m reading this  
correctly–

It took my version 27 seconds to do 6000 word\_wraps. Your regexp  
version did the same in 2.4 seconds 🙂

Jeff C.man

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [March 13, 2006, 2:46pm UTC](https://www.ruby-forum.com/t/word-wrap-method-for-string/80306/7 "2006-03-13T14:46:52Z")

</div>

everthing old is new again.  
you might also want to compare speeds. Yours might be faster.  
-lv

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [March 13, 2006, 3:29pm UTC](https://www.ruby-forum.com/t/word-wrap-method-for-string/80306/8 "2006-03-13T15:29:52Z")

</div>

On Mar 13, 2006, at 1:56 PM, Jeff C.man 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

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [March 13, 2006, 4:49pm UTC](https://www.ruby-forum.com/t/word-wrap-method-for-string/80306/9 "2006-03-13T16:49:43Z")

</div>

Lou V. wrote:

> here’s a (partially modded) excerpt from a previous discussion on this topic:
> 
> width = 11  
> str = ‘This is a test of the emergency broadcasting servicings I asseverate’  
> p str.scan(/\S.{0,#{width}}\S(?=\s|$)|\S+/)

When you modded, you didn’t notice that 8 was 2 less than the width.

str =  
‘This is a test of the emergency broadcasting servicings I asseverate’  
p str.scan(/\S.{0,#{width-2}}\S(?=\s|$)|\S+/)

---

<div class="post-metadata">

**Author:** ![jeff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e19b73/32.png) [@jeff](https://www.ruby-forum.com/u/jeff)\
**Post date:** [March 13, 2006, 3:08pm UTC](https://www.ruby-forum.com/t/word-wrap-method-for-string/80306/10 "2006-03-13T15:08:24Z")

</div>

yahtzee!  
btw, this isn’t my regex. Like most things of mine, they’re  
either cribbed, borrowed, or outright stolen.  
-lv
