Method: FatCore::String#wrap
- Defined in:
- lib/fat_core/string.rb
permalink #wrap(width = 70, hang = 0) ⇒ String
Return a string wrapped to width
characters with lines following the
first indented by hang
characters.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/fat_core/string.rb', line 51 def wrap(width = 70, hang = 0) result = '' first_line = true first_word_on_line = true line_width_so_far = 0 words = split(' ') words.each do |w| if !first_line && first_word_on_line w = ' ' * hang + w end unless first_word_on_line w = ' ' + w end result << w first_word_on_line = false line_width_so_far += 1 + w.length if line_width_so_far >= width result << "\n" line_width_so_far = 0 first_line = false first_word_on_line = true end end result.strip end |