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



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/string_tools.rb', line 48

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