On Sep 14, 2005, at 8:39 AM, Gavin Kistner wrote: > On Sep 14, 2005, at 8:26 AM, Erik Terpstra wrote: > >> str = 'This is a test of the emergency broadcasting services' >> str.scan(/(.{1,30})(?:\s+|$)/) >> >> => [["This is a test of the"], ["emergency broadcasting"], >> ["services"]] >> > > Nice. Not to golf, but how about simply: > > str = 'This is a test of the emergency broadcasting services' > p str.scan(/.{1,30}\b/) > #=> ["This is a test of the ", "emergency broadcasting ", "services"] Oops, because mine will split punctuation from its characters. However, both of ours will lose lines that are \S{31,} So: str = '123456789012345678901234567890This is a test of the emergency broadcasting system. This is only a test.' class String def wrap_to( col_width ) str = self.gsub( /(\S{#{col_width}})(\S)/, '\1 \2' ) str.scan(/(.{1,#{col_width}})(?:\s+|$)/).flatten.join( "\n" ) end end puts str.wrap_to( 30 ) 123456789012345678901234567890 This is a test of the emergency broadcasting system. This is only a test. puts str.wrap_to( 29 ) 12345678901234567890123456789 0This is a test of the emergency broadcasting system. This is only a test.