Ruby/String/wrap

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

(Различия между версиями)
Перейти к: навигация, поиск
м (1 версия)
 

Текущая версия на 17:56, 13 сентября 2010

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)