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