Class: String

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

Overview

Extend the string core class to add the truncate method from Rails

Instance Method Summary collapse

Instance Method Details

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



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

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