word_wrap
- 1.0.0 (0)
- 1.1.6 (0)
- 1.2.6 (13)
- 2.0.3 (17)
- 2.1.0 (0)
- 2.2.1 (38)
- 2.3.8 (0)
- 3.0.0 (0)
- 3.0.9 (-3)
- 3.1.0 (0)
- 3.2.1 (0)
- 3.2.8 (0)
- 3.2.13 (0)
- 4.0.2 (-15)
- 4.1.8 (0)
- 4.2.1 (0)
- 4.2.7 (0)
- 4.2.9 (0)
- 5.0.0.1 (12)
- 5.1.7 (0)
- 5.2.3 (0)
- 6.0.0 (0)
- 6.1.3.1 (0)
- 6.1.7.7 (0)
- 7.0.0 (0)
- 7.1.3.2 (1)
- 7.1.3.4 (0)
- What's this?
word_wrap(text, line_width: 80, break_sequence: "\n")
public
Wraps the text into lines no longer than line_width width. This method breaks on the first whitespace character that does not exceed line_width (which is 80 by default).
word_wrap('Once upon a time') # => "Once upon a time" word_wrap('Once upon a time, in a kingdom called Far Far Away, a king fell ill, and finding a successor to the throne turned out to be more trouble than anyone could have imagined...') # => "Once upon a time, in a kingdom called Far Far Away, a king fell ill, and finding\na successor to the throne turned out to be more trouble than anyone could have\nimagined..." word_wrap('Once upon a time', line_width: 8) # => "Once\nupon a\ntime" word_wrap('Once upon a time', line_width: 1) # => "Once\nupon\na\ntime"
You can also specify a custom break_sequence (“n” by default):
word_wrap('Once upon a time', line_width: 1, break_sequence: "\r\n") # => "Once\r\nupon\r\na\r\ntime"
Wrapping peculiarities
word_wrap will consume multiple line-breaks as well as leading & trailing line-breaks.
word_wrap("\nOnce upon a time\n\nThe End\n") # => Once upon a time\nThe End
word_wrap will NOT break long words
"supercalifragilisticexpialidocious".length # => 34 word_wrap("\nOnce upon a supercalifragilisticexpialidocious time", 15) # => Once upon a\nsupercalifragilisticexpialidocious\ntime
If you want a function that will break long words & maintain multiple line-breaks try this alternative. Note it does add a line break at the end of the output.
def breaking_wrap_wrap(txt, col = 80) txt.gsub(/(.{1,#{col}})( +|$\n?)|(.{1,#{col}})/, "\\1\\3\n") end breaking_wrap_wrap("\nOnce upon a supercalifragilisticexpialidocious time", 15) # => \nOnce upon a\nsupercalifragil\nisticexpialidoc\nious time\n
Regex-based code from http://blog.macromates.com/2006/wrapping-text-with-regular-expressions/
Wrapping peculiarities as of 2.x
In Rails 2.x word_wrap has been improved so that it no longer consumes multiple line-breaks or leading & trailing line-breaks.
word_wrap("\nOnce upon a time\n\nThe End\n") # => \nOnce upon a time\n\nThe End
However it still doesn’t break long words
"supercalifragilisticexpialidocious".length # => 30 word_wrap("\nOnce upon a supercalifragilisticexpialidocious time", 15) # => \nOnce upon a\nsupercalifragilisticexpialidocious\ntime
word_wrap with breaking long words
Code
def breaking_word_wrap(text, *args) options = args.extract_options! unless args.blank? options[:line_width] = args[0] || 80 end options.reverse_merge!(:line_width => 80) text = text.split(" ").collect do |word| word.length > options[:line_width] ? word.gsub(/(.{1,#{options[:line_width]}})/, "\\1 ") : word end * " " text.split("\n").collect do |line| line.length > options[:line_width] ? line.gsub(/(.{1,#{options[:line_width]}})(\s+|$)/, "\\1\n").strip : line end * "\n" end breaking_word_wrap("Once upon a supercalifragilisticexpialidocious time",15) # => Once upon a\nsupercalifragil\nisticexpialidoc\nious time