Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/string.rb

Instance Method Summary collapse

Instance Method Details

#truncate(truncate_at, options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/string.rb', line 4

def truncate(truncate_at, options = {})
  return dup unless length > truncate_at

  options[:omission] ||= '...'
  length_with_room_for_omission = truncate_at - options[:omission].length
  stop = if options[:separator]
           rindex(options[:separator], length_with_room_for_omission) || length_with_room_for_omission
         else
           length_with_room_for_omission
                end

  "#{self[0...stop]}#{options[:omission]}"
end