class WordWrapGem::Wrapper

Public Class Methods

wrap(s,c) click to toggle source
# File lib/word_wrap_gem.rb, line 6
def self.wrap (s,c)
  raise ArgumentError if (c < 1 || c%1!=0)
  s = s.to_s
  s = s.strip
  if s.length > c
    where_to_break = s.slice(0, c).rindex(" ") || c
    num_characters_remain = s.length - where_to_break
    return s.slice(0, where_to_break) + "\n" + wrap(s.slice(where_to_break, num_characters_remain).strip, c)
  end
  s
end