module StringTools::WordProcessing

Public Instance Methods

truncate_words(text, length = 75) click to toggle source
# 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