Wrap a string with new line sing : wrap « String « Ruby






Wrap a string with new line sing


def wrap(s, width=78)
  s.gsub(/(.{1,#{width}})(\s+|\Z)/, "\\1\n")
end


poetry = %q{It is an ancient Mariner,
A
"}

puts wrap(poetry, 20)

 








Related examples in the same category

1.Word-wrapping Lines of Text