J
Jeff Coleman
Hi all,
For a text adventure program I'm working on I needed a method to word
wrap a long string based on spaces or non-word characters so I came up
with this. I just thought I'd post it to see if there are any thoughts
or if there might be a better way to do it.
class String
def word_wrap(width)
source = self.dup
original_width = width
while width < source.length do
last_space = source.rindex( / |\W/, width )
source.insert( last_space, "\n" )
source.gsub!(/\n */,"\n")
width = last_space + original_width
end
source
end
end
Jeff Coleman
For a text adventure program I'm working on I needed a method to word
wrap a long string based on spaces or non-word characters so I came up
with this. I just thought I'd post it to see if there are any thoughts
or if there might be a better way to do it.
class String
def word_wrap(width)
source = self.dup
original_width = width
while width < source.length do
last_space = source.rindex( / |\W/, width )
source.insert( last_space, "\n" )
source.gsub!(/\n */,"\n")
width = last_space + original_width
end
source
end
end
Jeff Coleman