From: why the lucky stiff Date: 2004-02-15T16:58:12+09:00 Subject: Re: method to WRAP text at _ # of characters? Ruby Baby wrote: > > Having taken PHP's "wordwrap" function for granted, > (), 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