Class: String

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

Instance Method Summary collapse

Instance Method Details

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

taken from rails



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

def truncate(truncate_at, options = {})
  s = self.gsub(/\r\n/, ' ')
  return s unless length > truncate_at

  omission = options[:omission] || '...'
  length_with_room_for_omission = truncate_at - 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

  "#{s[0, stop]}#{omission}"
end