Class: Wrappers::Delight::WordWrap

Inherits:
Object
  • Object
show all
Defined in:
lib/wrappers/delight.rb

Instance Method Summary collapse

Instance Method Details

#wrap(string, max_width) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/wrappers/delight.rb', line 7

def wrap(string, max_width)
  return string if string.length <= max_width
  if string[0...max_width].index(" ") != nil
    space_pos = string.rindex(" ")
    string[0...space_pos] + "\n" + wrap(string[space_pos+1..-1],max_width)
  elsif string[max_width]==" "
    string[0...max_width] + "\n" + wrap(string[max_width+1..-1], max_width)
  else
     string[0...max_width] + "\n" + wrap(string[max_width..-1],max_width)
  end
end