# Text wrap

**URL:** <https://www.ruby-forum.com/t/text-wrap/78888>\
**Category:** Ruby\
**Created:** [December 10, 2006, 1:58am UTC](https://www.ruby-forum.com/t/text-wrap/78888 "2006-12-10T01:58:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ishamid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/ad7895/32.png) [@ishamid](https://www.ruby-forum.com/u/ishamid)\
**Post date:** [December 10, 2006, 1:58am UTC](https://www.ruby-forum.com/t/text-wrap/78888/1 "2006-12-10T01:58:27Z")

</div>

Hi,

I have a ruby script that’s doing what I need, and I would like to add  
one feature. The script outputs a text file, and I would like Ruby to  
take the final text file and wrap it at 67 characters, instead of doing  
it manually in the editor.

Is there an available method or something that I can invoke? If so, can  
you give me an example of its use (I’m still very much a novice!)

Best  
Idris

---

<div class="post-metadata">

**Author:** ![ishamid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/ad7895/32.png) [@ishamid](https://www.ruby-forum.com/u/ishamid)\
**Post date:** [December 10, 2006, 2:06am UTC](https://www.ruby-forum.com/t/text-wrap/78888/2 "2006-12-10T02:06:28Z")

</div>

text = whatever you need to wrap

To print it:  
0.step(text.length, 67) {|x| puts text[x, x+67]}

To put it in another variable:  
wrapped = “”  
0.step(text.length, 67) {|x| wrapped \<\< “\n” \<\< text[x, x+67]}

Dan

---

<div class="post-metadata">

**Author:** ![ishamid](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/ad7895/32.png) [@ishamid](https://www.ruby-forum.com/u/ishamid)\
**Post date:** [December 10, 2006, 2:46am UTC](https://www.ruby-forum.com/t/text-wrap/78888/3 "2006-12-10T02:46:32Z")

</div>

ishamid wrote:

> Best  
> Idris

X = 10 # Width is 10 characters.  
str =  
“This\nis a test of the emergency broadcasting servicings I  
asseverate”  
p str.gsub(/\n/," ").scan(/\S.{0,#{X-2}}\S(?=\s|$)|\S+/)
