module T::Utils

def self.string_truncate_middle(str, start_len, end_len, ellipsis='...')

Returns:
  • (String) -

Parameters:
  • ellipsis (String) -- The string to add in place of the elided text
  • end_len (Fixnum) -- The length of string after the ellipsis
  • start_len (Fixnum) -- The length of string before the ellipsis
  • str (String) --
def self.string_truncate_middle(str, start_len, end_len, ellipsis='...')
  return unless str
  raise ArgumentError.new('must provide start_len') unless start_len
  raise ArgumentError.new('must provide end_len') unless end_len
  raise ArgumentError.new('start_len must be >= 0') if start_len < 0
  raise ArgumentError.new('end_len must be >= 0') if end_len < 0
  str = str.to_s
  return str if str.length <= start_len + end_len
  start_part = str[0...start_len - ellipsis.length]
  end_part = end_len == 0 ? '' : str[-end_len..-1]
  "#{start_part}#{ellipsis}#{end_part}"
end