Method: Howzit::StringUtils#trunc

Defined in:
lib/howzit/stringutils.rb

#trunc(len) ⇒ Object

Truncate string to nearest word

Parameters:

  • len (Integer)

    max length of string



164
165
166
167
168
169
170
# File 'lib/howzit/stringutils.rb', line 164

def trunc(len)
  split(/ /).each_with_object([]) do |x, ob|
    break ob unless ob.join(' ').length + ' '.length + x.length <= len

    ob.push(x)
  end.join(' ').strip
end