Class: String

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

Overview


Instance Method Summary collapse

Instance Method Details

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

call-seq:

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

Reduce 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)


58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/logging/utils.rb', line 58

def reduce( 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