Word-wrapping Lines of Text
def wrap(s, width=78)
s.gsub(/(.{1,#{width}})(\s+|\Z)/, "\\1\n")
end
wrap("This text is too short to be wrapped.")
puts wrap("This text is not too short to be wrapped.", 20)
puts wrap("this is a test. this is a test. this is a test. this is a test", 10)
Related examples in the same category