# Using Ruby for line-wrapping

**URL:** <https://www.ruby-forum.com/t/using-ruby-for-line-wrapping/93089>\
**Category:** Ruby\
**Created:** [April 27, 2007, 11:27am UTC](https://www.ruby-forum.com/t/using-ruby-for-line-wrapping/93089 "2007-04-27T11:27:17Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cpeterson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58f4c7/32.png) [@cpeterson](https://www.ruby-forum.com/u/cpeterson)\
**Post date:** [April 27, 2007, 11:27am UTC](https://www.ruby-forum.com/t/using-ruby-for-line-wrapping/93089/1 "2007-04-27T11:27:17Z")

</div>

I don’t necessarily need code examples – but if anyone has ideas for a  
best approach to specifying a line wrap width (breaking between words  
for lines no longer than a specific column width) for output from a Ruby  
script, I’d love to hear about it.

---

<div class="post-metadata">

**Author:** ![cpeterson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58f4c7/32.png) [@cpeterson](https://www.ruby-forum.com/u/cpeterson)\
**Post date:** [April 27, 2007, 11:39am UTC](https://www.ruby-forum.com/t/using-ruby-for-line-wrapping/93089/2 "2007-04-27T11:39:18Z")

</div>

On 4/27/07, Chad P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I don’t necessarily need code examples – but if anyone has ideas for a  
> best approach to specifying a line wrap width (breaking between words  
> for lines no longer than a specific column width) for output from a Ruby  
> script, I’d love to hear about it.

Check out Text::Format

martin

---

<div class="post-metadata">

**Author:** ![cpeterson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58f4c7/32.png) [@cpeterson](https://www.ruby-forum.com/u/cpeterson)\
**Post date:** [April 27, 2007, 11:47am UTC](https://www.ruby-forum.com/t/using-ruby-for-line-wrapping/93089/3 "2007-04-27T11:47:19Z")

</div>

On Fri, Apr 27, 2007 at 06:38:45PM +0900, Martin DeMello wrote:

> On 4/27/07, Chad P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I don’t necessarily need code examples – but if anyone has ideas for a  
> > best approach to specifying a line wrap width (breaking between words  
> > for lines no longer than a specific column width) for output from a Ruby  
> > script, I’d love to hear about it.
> 
> Check out Text::Format

Thanks. I’ll give it a look.

---

<div class="post-metadata">

**Author:** ![cpeterson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58f4c7/32.png) [@cpeterson](https://www.ruby-forum.com/u/cpeterson)\
**Post date:** [April 27, 2007, 12:23pm UTC](https://www.ruby-forum.com/t/using-ruby-for-line-wrapping/93089/4 "2007-04-27T12:23:21Z")

</div>

On Fri, Apr 27, 2007 at 06:50:06PM +0900, William J. wrote:

> X = 40  
> puts str.gsub(/\n/," ").scan(/\S.{0,#{X-2}}\S(?=\s|$)|\S+/)

Thanks. The scan method’s pretty nice. I had no idea it existed.

---

<div class="post-metadata">

**Author:** ![cpeterson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58f4c7/32.png) [@cpeterson](https://www.ruby-forum.com/u/cpeterson)\
**Post date:** [April 27, 2007, 2:59pm UTC](https://www.ruby-forum.com/t/using-ruby-for-line-wrapping/93089/5 "2007-04-27T14:59:12Z")

</div>

On Fri, Apr 27, 2007 at 06:25:24PM +0900, Chad P. wrote:

> I don’t necessarily need code examples – but if anyone has ideas for a  
> best approach to specifying a line wrap width (breaking between words  
> for lines no longer than a specific column width) for output from a Ruby  
> script, I’d love to hear about it.

You can use prettyprint:

irb(main):001:0\> require ‘prettyprint’  
=\> false  
irb(main):003:0\> s = “The quick brown fox jumped over the lazy dog. Now  
is the time for all good men to come to the aid of their country. You  
have been warned.”  
irb(main):015:0\> puts PrettyPrint.format(’’, 80) { |q|  
irb(main):016:1\* s.scan(/\S+/) { |t|  
irb(main):017:2\* q.group { q.text t; q.breakable }  
irb(main):018:2\> }  
irb(main):019:1\> }  
The quick brown fox jumped over the lazy dog. Now is the time for all  
good men  
to come to the aid of their country. You have been warned.  
=\> nil

I get the feeling I’m not doing this quite right, since this strips out  
newlines and doesn’t retain multiple spaces from the original string.  
Perhaps there’s a prettyprint expert out there who knows how to do this  
better.

Paul

---

<div class="post-metadata">

**Author:** ![cpeterson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58f4c7/32.png) [@cpeterson](https://www.ruby-forum.com/u/cpeterson)\
**Post date:** [April 27, 2007, 11:51am UTC](https://www.ruby-forum.com/t/using-ruby-for-line-wrapping/93089/6 "2007-04-27T11:51:19Z")

</div>

On Apr 27, 4:25 am, Chad P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I don’t necessarily need code examples – but if anyone has ideas for a  
> best approach to specifying a line wrap width (breaking between words  
> for lines no longer than a specific column width) for output from a Ruby  
> script, I’d love to hear about it.
> 
> –  
> CCD CopyWrite Chad P. [[http://ccd.apotheon.org](http://ccd.apotheon.org)]  
> Paul Graham: “Real ugliness is not harsh-looking syntax, but having to  
> build programs out of the wrong concepts.”

str = "  
I don’t necessarily need code examples – but if anyone has  
ideas for a best approach to specifying a line wrap width  
(breaking between words for lines no longer than a specific  
column width) for output from a Ruby script, I’d love to  
hear about it."

X = 40  
puts str.gsub(/\n/," ").scan(/\S.{0,#{X-2}}\S(?=\s|$)|\S+/)

— output —  
I don’t necessarily need code examples  
– but if anyone has ideas for a best  
approach to specifying a line wrap width  
(breaking between words for lines no  
longer than a specific column width) for  
output from a Ruby script, I’d love to  
hear about it.

---

<div class="post-metadata">

**Author:** ![cpeterson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58f4c7/32.png) [@cpeterson](https://www.ruby-forum.com/u/cpeterson)\
**Post date:** [April 28, 2007, 7:45am UTC](https://www.ruby-forum.com/t/using-ruby-for-line-wrapping/93089/7 "2007-04-28T07:45:49Z")

</div>

On Apr 27, 3:25 am, Chad P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I don’t necessarily need code examples – but if anyone has ideas for a  
> best approach to specifying a line wrap width (breaking between words  
> for lines no longer than a specific column width) for output from a Ruby  
> script, I’d love to hear about it.

See question #5 in Quiz #113:  
[http://www.rubyquiz.com/quiz113.html](http://www.rubyquiz.com/quiz113.html)

Also relevant are some of the regexp used in the thread “halving a  
string”:  
[http://groups.google.com/group/comp.lang.ruby/browse\_frm/thread/b14f2e9a15eaed9b/f94e696b8b9ba2d9?lnk=gst&q=halving+a+string&rnum=1#f94e696b8b9ba2d9](http://groups.google.com/group/comp.lang.ruby/browse_frm/thread/b14f2e9a15eaed9b/f94e696b8b9ba2d9?lnk=gst&q=halving+a+string&rnum=1#f94e696b8b9ba2d9)
