Class: String

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

Overview


Instance Method Summary collapse

Instance Method Details

#shrink(width, ellipses = '...') ⇒ Object

call-seq:

shrink( width, ellipses = '...' )    #=> string

Shrink the size of the current string to the given width by removing characters from the middle of the string and replacing them with ellipses. If the width is greater than the length of the string, the string is returned unchanged. If the width is less than the length of the ellipses, then the ellipses are returned.

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/logging/utils.rb', line 17

def shrink( width, ellipses = '...')
  raise ArgumentError, "width cannot be negative: #{width}" if width < 0

  return self if length <= width

  remove = length - width + ellipses.length
  return ellipses.dup if remove >= length

  left_end = (length + 1 - remove) / 2
  right_start = left_end + remove

  left = self[0,left_end]
  right = self[right_start,length-right_start]

  left << ellipses << right
end