Ruby/String/wrap

Материал из Wiki.crossplatform.ru

Перейти к: навигация, поиск

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)



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)