Method: Doing::StringTruncate#truncend

Defined in:
lib/doing/string/truncate.rb

#truncend(len, ellipsis: '...') ⇒ Object

Truncate from middle to end at nearest word

Parameters:

  • len

    The length



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/doing/string/truncate.rb', line 37

def truncend(len, ellipsis: '...')
  return self if length <= len

  total = 0
  res = []

  split(/ /).reverse.each do |word|
    break if total + 1 + word.length > len

    total += 1 + word.length
    res.unshift(word)
  end
  ellipsis + res.join(' ')
end