Re: Wrapping strings
- From: Mark J. Reed <mreed@xxxxxxxxxxxx>
- Date: Tue, 04 Oct 2005 03:05:09 GMT
Ben <benbelly@xxxxxxxxx> writes:
>I have a string containing multiple lines that I would like to put
>into a C++ file as a comment:
>desc =3D ""
>while (line =3D gets) !~ /^\.$/
> desc +=3D "#{line}"
>end
>I'd like to precede each line with a "// " to make it a comment, and
>I'd like to wrap each line by the 78th character.
>I don't suppose there is any way to do this quick and simple like?
>I figure I need to pull out the carriage returns, but then I can't
>think of anything pretty after that. Any tips?
I recommend getting the text-format module (available from the RAA
at http://rubyforge.org/projects/text-format or as a gem) and using it.
If that doesn't fit your needs for some reason, you can do it manually.
Something like this should work:
words = line.split(/\s+/)
while words do
line = '// '
while words && line.length < 78 do
line += words.shift + ' '
end
puts line
end
>-Ben
.
- Follow-Ups:
- Re: Wrapping strings
- From: Mark J . Reed
- Re: Wrapping strings
- References:
- Wrapping strings
- From: Ben
- Wrapping strings
- Prev by Date: Re: Type inference
- Next by Date: Re: Wrapping strings
- Previous by thread: Wrapping strings
- Next by thread: Re: Wrapping strings
- Index(es):
Relevant Pages
|