def to_s(format = :long, options = nil)
      format =
        case format
        when Symbol
          FORMATS.fetch(format)
        when Hash
          FORMATS.fetch(:long).merge(format)
        else
          raise ArgumentError, "Expected #{format.inspect} to be a Symbol or Hash"
        end

      format = format.merge(options || {})

      count =
        if format[:count].nil? || format[:count] == :all
          UNITS.count
        else
          format[:count]
        end

      format_units = format.fetch(:units)
      units = self.to_units(*format_units.keys).select {|unit, n| n > 0}
      if units.empty?
        units = {seconds: 0}
      end

      separator = format[:separator] || ' '
      delimiter = format[:delimiter] || ', '
      units.take(count).map do |unit, n|
        unit_label = format_units.fetch(unit)

        singular, plural =
          case unit_label
          when Array
            unit_label
          else
            [unit_label, unit_label]
          end

          unit_name =
            if n == 1
              singular
            else
              plural || singular
            end

          [n, unit_name].join(separator)
      end.join(format[:delimiter] || ', ')
    end
def to_s(format = :long, options = nil)
      format =
        case format
        when Symbol
          FORMATS.fetch(format)
        when Hash
          FORMATS.fetch(:long).merge(format)
        else
          raise ArgumentError, "Expected #{format.inspect} to be a Symbol or Hash"
        end

      format = format.merge(options || {})

      count =
        if format[:count].nil? || format[:count] == :all
          UNITS.count
        else
          format[:count]
        end

      format_units = format.fetch(:units)
      units = self.to_units(*format_units.keys).select {|unit, n| n > 0}
      if units.empty?
        units = {seconds: 0}
      end

      separator = format[:separator] || ' '
      delimiter = format[:delimiter] || ', '
      units.take(count).map do |unit, n|
        unit_label = format_units.fetch(unit)

        singular, plural =
          case unit_label
          when Array
            unit_label
          else
            [unit_label, unit_label]
          end

          unit_name =
            if n == 1
              singular
            else
              plural || singular
            end

          [n, unit_name].join(separator)
      end.join(format[:delimiter] || ', ')
    end
Convert a duration to a human-readable string.

 @param [Symbol, Hash] format The format type to format the duration with.
   `format` can either be a key from the {FORMATS} hash or a hash with
   the same shape as `options`.
 @param [Hash, nil] options Additional options to use to override default
   format options.

 @option options [Hash<Symbol, String>] :units The full list of unit names
   to use. Keys are unit names (see {UNIT_ALIASES} for a full list) and
   values are strings to use when converting that unit to a string. Values
   can also be an array, where the first item of the array will be used
   for singular unit names and the second item will be used for plural
   unit names. Note that this option will completely override the input
   formats' list of names, so all units that should be used must be
   specified!
 @option options [String] :separator The separator to use between a unit
   quantity and the unit's name. For example, the string `"1 second"` uses
   a separator of `" "`.
 @option options [String] :delimiter The delimiter to use between separate
   units. For example, the string `"1 minute, 1 second"` uses a separator
   of `", "`
 @option options [Integer, nil, :all] :count The number of significant
   units to use in the string, or `nil` / `:all` to use all units.
   For example, if the given duration is `1.day 1.week 1.month`, and
   `options[:count]` is 2, then the resulting string will only include
   the month and the week components of the string.

 @return [String] The duration formatted as a string.