public Method

TextHelper.word_wrap(text, line_width = 80)

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).

Examples

word_wrap('Once upon a time', 4)
# => Once\nupon\na\ntime

word_wrap('Once upon a time', 8)
# => Once upon\na time

word_wrap('Once upon a time')
# => Once upon a time

word_wrap('Once upon a time', 1)
# => Once\nupon\na\ntime

Source Code

# File action_view/helpers/text_helper.rb, line 195
def word_wrap(text, line_width = 80)
  text.split("\n").collect do |line|
    line.length > line_width ? line.gsub(/(.{1,#{line_width}})(\s+|$)/, "\\1\n").strip : line
  end * "\n"
end
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.