Professional Web Applications Themes

method to WRAP text at _ # of characters? - Ruby

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

  1. #1

    Default method to WRAP text at _ # of characters?

    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?



    Ruby Guest

  2. #2

    Default Re: method to WRAP text at _ # of characters?

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

    On Sun, Feb 15, 2004 at 04:11:36PM +0900, Ruby Baby wrote: 

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


    Zachary Guest

  3. #3

    Default Re: method to WRAP text at _ # of characters?

    Ruby Baby wrote: 

    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




    why Guest

  4. #4

    Default Re: method to WRAP text at _ # of characters?

    On Sunday, February 15, 2004, 6:11:36 PM, Ruby wrote:
     
     
     
     


    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



    Gavin Guest

Similar Threads

  1. Text wrap adding a ghost Wrap???
    By barbarapress@adobeforums.com in forum Adobe Indesign Macintosh
    Replies: 7
    Last Post: July 6th, 09:24 PM
  2. Text Wrap around an image pasted into a text box
    By Jerry_Mayeroff@adobeforums.com in forum Adobe Indesign Windows
    Replies: 0
    Last Post: April 1st, 05:42 PM
  3. Text::Wrap::wrap difference
    By Art in forum PERL Miscellaneous
    Replies: 2
    Last Post: September 25th, 06:15 PM
  4. Replies: 1
    Last Post: August 12th, 03:28 PM
  5. WRAP text in <TD>
    By Sara T. in forum ASP.NET General
    Replies: 0
    Last Post: July 26th, 09:05 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139