Class: String

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

Overview

Pulled from ActionSupport.

Instance Method Summary collapse

Instance Method Details

#at_width(width) ⇒ Object



52
53
54
# File 'lib/utils.rb', line 52

def at_width(width)
  truncate(width).ljust(width)
end

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



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

def truncate(truncate_at, options = {})
  return dup 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

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