[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

string#fmt - breaking lines (no indent

Josef 'Jupp' Schugt

4/28/2005 4:44:00 PM

Hi!

After some trouble with mail programs not supporting "format=flowed" I
wrote a quick hack that line-breaks strings but keeps words intact
that exceed the intended line-length (currently hard-wired to 72 - I
already wrote that it is a quick hack, didn't I?). No warranty of any
kind. Improvements welcome. I did not forget to implement indentations
- the strings that I apply String#fmt to simply don't have them.

class String
def fmt
maxlen = 72
result = ""
temp = ""
gsub(/[ \s]+/, ' ').strip.scan(/[^ ]+/) { |x|
if temp == ""
temp << x
else
if temp.length + x.length < maxlen
temp << " " + x
else
result << temp + "\n"
temp = ""
redo
end
end
}
result + temp
end
end

Josef 'Jupp' Schugt


2 Answers

Ara.T.Howard

4/28/2005 5:02:00 PM

0

nobu.nokada

4/29/2005 3:07:00 AM

0

Hi,

At Fri, 29 Apr 2005 01:44:17 +0900,
Josef 'Jupp' SCHUGT wrote in [ruby-talk:140291]:
> After some trouble with mail programs not supporting "format=flowed" I
> wrote a quick hack that line-breaks strings but keeps words intact
> that exceed the intended line-length (currently hard-wired to 72 - I
> already wrote that it is a quick hack, didn't I?). No warranty of any
> kind. Improvements welcome. I did not forget to implement indentations
> - the strings that I apply String#fmt to simply don't have them.

wrap.rb <http://www.ruby-lang.org/cgi-bin/cvsweb.cgi/rough/lib/w...

--
Nobu Nakada