Module: StringTools::WordProcessing

Included in:
StringTools
Defined in:
lib/string_tools.rb

Instance Method Summary collapse

Instance Method Details

#truncate_words(text, length = 75) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/string_tools.rb', line 40

def truncate_words(text, length = 75)
  return if text.nil?

  if text.mb_chars.size > length
    new_length = text.mb_chars[0...length].rindex(/[^[:word:]]/)
    text.mb_chars[0...new_length.to_i]
  else
    text
  end
rescue
  text[0...length]
end