method to WRAP text at _ # of characters?

R

Ruby Baby

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>), 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?
 
Z

Zachary P. Landau

--ibTvN161/egqYuK8
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

Made myself a li'l Ruby class for sending email.
=20
But of course I'd like to wrap the outgoing body of text at
68 characters or so, before sending.
=20
Having taken PHP's "wordwrap" function for granted,
(<http://us2.php.net/wordwrap>), I was surprised not to find
a similar method built-in to Ruby.
=20
Or is it?
Did I miss it?
Am I going to have to write my own?

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

--
Zachary P. Landau <[email protected]>
GPG: gpg --recv-key 0x24E5AD99 | http://kapheine.hypa.net/kapheine.asc

--ibTvN161/egqYuK8
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFALyYDCwWyMCTlrZkRAtzuAKCEgUqc/EYwAul8+3Gl1Ed8D77PtACeMHyZ
25xWJiTSn+qsNmHakw8+7CU=
=Uf8A
-----END PGP SIGNATURE-----

--ibTvN161/egqYuK8--
 
W

why the lucky stiff

Ruby said:
Having taken PHP's "wordwrap" function for granted,
(<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
 
G

Gavin Sinclair

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>), 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?


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,143
Messages
2,570,821
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top