instance method
word_wrap
Ruby on Rails 3.0.20
Since v2.2.3Signature
word_wrap(text, *args)
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') # => 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\n a successor to the throne turned out to be more trouble than anyone could have\n imagined... word_wrap('Once upon a time', :line_width => 8) # => Once upon\na time word_wrap('Once upon a time', :line_width => 1) # => Once\nupon\na\ntime
You can still use word_wrap with the old API that accepts the line_width as its optional second parameter:
word_wrap('Once upon a time', 8) # => Once upon\na time
Parameters
-
textreq -
argsrest
Source
# File actionpack/lib/action_view/helpers/text_helper.rb, line 219
def 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.split("\n").collect do |line|
line.length > options[:line_width] ? line.gsub(/(.{1,#{options[:line_width]}})(\s+|$)/, "\\1\n").strip : line
end * "\n"
end
Defined in actionpack/lib/action_view/helpers/text_helper.rb line 219
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActionView::Helpers::TextHelper