# Method to WRAP text at \_ # of characters?

**URL:** <https://rubytalk.org/t/method-to-wrap-text-at-of-characters/10167>\
**Category:** ruby-talk\
**Created:** [15 February 2004 07:11 UTC](https://rubytalk.org/t/method-to-wrap-text-at-of-characters/10167 "2004-02-15T07:11:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ruby\_Baby](https://avatars.discourse-cdn.com/v4/letter/r/47e85d/32.png) [@Ruby\_Baby](https://rubytalk.org/u/Ruby_Baby)\
**Post date:** [15 February 2004 07:11 UTC](https://rubytalk.org/t/method-to-wrap-text-at-of-characters/10167/1 "2004-02-15T07:11:36Z")

</div>

Made myself a li’l Ruby class for sending email.

But of course I’d like to wrap the outgoing body of text at  
68 characters or so, before sending.

Having taken PHP’s “wordwrap” function for granted,  
([http://us2.php.net/wordwrap](http://us2.php.net/wordwrap)), I was surprised not to find  
a similar method built-in to Ruby.

Or is it?  
Did I miss it?  
Am I going to have to write my own?

---

<div class="post-metadata">

**Author:** ![Zachary\_P\_Landau](https://avatars.discourse-cdn.com/v4/letter/z/e99b99/32.png) [@Zachary\_P\_Landau](https://rubytalk.org/u/Zachary_P_Landau)\
**Post date:** [15 February 2004 07:58 UTC](https://rubytalk.org/t/method-to-wrap-text-at-of-characters/10167/2 "2004-02-15T07:58:06Z")

</div>

Try searching for “gsub \1 \n” in the comp.lang.ruby search bar at  
[http://www.rubyforge.org](http://www.rubyforge.org). The first result is a discussion about a wrap  
method.

> **···**
>
> On Sun, Feb 15, 2004 at 04:11:36PM +0900, Ruby Baby wrote:
> 
> > Made myself a li’l Ruby class for sending email.
> > 
> > But of course I’d like to wrap the outgoing body of text at  
> > 68 characters or so, before sending.
> > 
> > Having taken PHP’s “wordwrap” function for granted,  
> > ([http://us2.php.net/wordwrap](http://us2.php.net/wordwrap)), I was surprised not to find  
> > a similar method built-in to Ruby.
> > 
> > Or is it?  
> > Did I miss it?  
> > Am I going to have to write my own?
> 
> –  
> Zachary P. Landau [kapheine@hypa.net](mailto:kapheine@hypa.net)  
> GPG: gpg --recv-key 0x24E5AD99 | [http://kapheine.hypa.net/kapheine.asc](http://kapheine.hypa.net/kapheine.asc)

---

<div class="post-metadata">

**Author:** ![why\_the\_lucky\_stiff1](https://avatars.discourse-cdn.com/v4/letter/w/898d66/32.png) [@why\_the\_lucky\_stiff1](https://rubytalk.org/u/why_the_lucky_stiff1)\
**Post date:** [15 February 2004 07:58 UTC](https://rubytalk.org/t/method-to-wrap-text-at-of-characters/10167/3 "2004-02-15T07:58:12Z")

</div>

Ruby Baby wrote:

> Having taken PHP’s “wordwrap” function for granted,  
> ([http://us2.php.net/wordwrap](http://us2.php.net/wordwrap)), I was surprised not to find  
> a similar method built-in to Ruby.

Here’s a simple one I like to use:

# Wrapping strings for display

class String  
def wordwrap( len )  
gsub( /\n/, “\n\n” ).gsub( /(.{1,#{len}})(\s+|$)/, “\1\n” )  
end  
end

s = “Insert unwrapped and elaborate, exhaustive tomes here.”  
s.wordwrap( 68 )  
#=\> outputs string wrapped to 68 columns

However, the String#wordwrap method is rather limited, so if I ever end  
up needing something more, I use the Text::Format[1] module.

require ‘text/format’

formatter = Text::Format.new {  
@columns = 68  
@first\_indent = 0  
}

s = “Insert unwrapped and elaborate, exhaustive tomes here.”  
formatter.format( s )

Yip.

\_why

[1] [http://raa.ruby-lang.org/list.rhtml?name=text-format](http://raa.ruby-lang.org/list.rhtml?name=text-format)

---

<div class="post-metadata">

**Author:** ![Gavin\_Sinclair](https://avatars.discourse-cdn.com/v4/letter/g/db5fbb/32.png) [@Gavin\_Sinclair](https://rubytalk.org/u/Gavin_Sinclair)\
**Post date:** [15 February 2004 10:08 UTC](https://rubytalk.org/t/method-to-wrap-text-at-of-characters/10167/4 "2004-02-15T10:08:43Z")

</div>

There’s a wrap implementation in ‘rough’ (a CVS repository of  
candiates for the Ruby standard library), as well as a few other text  
manipulation things. I’d like to see them all become standard,  
although better organised than they are now.

Gavin

> **···**
>
> On Sunday, February 15, 2004, 6:11:36 PM, Ruby wrote:
> 
> > Made myself a li’l Ruby class for sending email.
> 
> > But of course I’d like to wrap the outgoing body of text at  
> > 68 characters or so, before sending.
> 
> > Having taken PHP’s “wordwrap” function for granted,  
> > ([http://us2.php.net/wordwrap](http://us2.php.net/wordwrap)), I was surprised not to find  
> > a similar method built-in to Ruby.
> 
> > Or is it?  
> > Did I miss it?  
> > Am I going to have to write my own?
