From: David Alan Black Date: 2002-08-01T15:08:48+09:00 Subject: Re: How to make a virtual 2nd column!!! Hi -- On Thu, 1 Aug 2002, Kurt Euler wrote: > All- > > Here's a record formatting question for you: > > Say I have a record like in this format... > > nnnnnThis is a sample sentence meant to demonstrate desired word wrapping to make the appearance of a virtual column from a single long string that doesn't have any hard returns. > > ... and I want to format it to look something like this: > > nnnnn This is a sample sentence meant to > demonstrate desired word wrapping > to make the appearance of a virtual > column from a single long string > that doesn't have any hard returns. > > where: > > - nnnnn is a 5 character string. > - is a tab character (to be replaced by three spaces). > - The first space after every 35 or so characters in the sentence > (2nd field of the record) is replaced by a hard return plus eight > spaces. > - The sample above is one of many records in a file, separated by a > hard return. OK, a first shot at it: # Very quick, home-rolled text wrapper (there are others out # there somewhere) class String def wrap(cols=80) gsub!(/\n/," ") gsub!(/.{1,#{cols}}(?:\s|\z)/) { |s| "#{s}\n" } end end # The leading spaces effect def hanging_indent(tag,text,cols=25) w = text.wrap(cols).gsub(/^\s*/," " * (tag.size + 3)) w[0..tag.size-1] = tag w end w = "This is a sample sentence meant to demonstrate desired word wrapping to make the appearance of a virtual column from a single long string that doesn't have any hard returns." puts hanging_indent("nnnnn",w,35) gives me: nnnnn This is a sample sentence meant to demonstrate desired word wrapping to make the appearance of a virtual column from a single long string that doesn't have any hard returns. David -- David Alan Black home: dblack@candle.superlink.net work: blackdav@shu.edu Web: http://pirate.shu.edu/~blackdav