redis/redis-rb · error · ArgumentError

ranges must be given as start/stop pairs

Error message

ranges must be given as start/stop pairs

What it means

ardelrange accepts one or more inclusive start/stop index ranges, as flat alternating integers or one array per range. It flattens the arguments, maps each through Integer(), and raises ArgumentError 'ranges must be given as start/stop pairs' when the list is empty or odd-length — a start bound without its stop.

Source

Thrown at lib/redis/commands/arrays.rb:165

      # Delete elements in one or more inclusive index ranges.
      #
      # Ranges may overlap; each element is counted at most once. A range
      # given with `start > stop` is processed in ascending order regardless.
      #
      # @example Delete two ranges
      #   redis.ardelrange("foo", 0, 2, 5, 7)
      #     # => 6
      # @example Ranges as pairs
      #   redis.ardelrange("foo", [0, 2], [5, 7])
      #     # => 6
      #
      # @param [String] key
      # @param [Integer, Array<Integer>] ranges one or more start/stop pairs,
      #   as flat alternating integers or one array per range
      # @return [Integer] the number of elements deleted
      def ardelrange(key, *ranges)
        ranges = ranges.flatten(1).map { |index| Integer(index) }
        raise ArgumentError, "ranges must be given as start/stop pairs" if ranges.empty? || ranges.size.odd?

        send_command([:ardelrange, key, *ranges])
      end

      # Insert one or more values at consecutive indices, beginning at the
      # array's insert cursor. The cursor advances by one for each value.
      #
      # @see #arnext inspect the current cursor position
      # @see #arseek reposition the cursor
      #
      # @param [String] key
      # @param [String] values one or more values to insert
      # @return [Integer] the last index where a value was inserted
      def arinsert(key, *values)
        send_command([:arinsert, key, *values])
      end

      # Set the insert cursor of an array to a specific index.

View on GitHub (pinned to 2ba9010b91)

Solutions

  1. Supply complete pairs: ardelrange(key, 0, 2, 5, 7) or ardelrange(key, [0, 2], [5, 7])
  2. When carrying Ruby Range objects, convert each to [range.begin, range.end] first
  3. Validate dynamic bounds before calling: flattened list must be non-empty and even-length

Example fix

# before
redis.ardelrange('foo', 0, 2, 5)  # missing stop: ArgumentError

# after
redis.ardelrange('foo', 0, 2, 5, 7)
redis.ardelrange('foo', [0, 2], [5, 7])  # pair form
Defensive patterns

Strategy: validation

Validate before calling

bounds = ranges.flatten(1)
raise ArgumentError, 'need start/stop pairs' if bounds.empty? || bounds.size.odd?
redis.ardelrange(key, *bounds)

Try / catch

begin
  redis.ardelrange(key, *bounds)
rescue ArgumentError => e
  raise ArgumentError, "bad ranges (#{bounds.inspect}): #{e.message}"
end

Prevention

When it happens

Trigger: redis.ardelrange('foo') with no ranges; redis.ardelrange('foo', 0, 2, 5) — odd count; passing [ [0, 2], [5] ] where an inner array holds one element. (Non-integer strings fail earlier inside Integer() with a different error.)

Common situations: Ranges assembled at runtime (user-selected ranges, batched deletions) where the last pair was truncated; off-by-one when slicing a flat bounds array; converting Range objects and forgetting to emit both begin and end.

Related errors


AI-assisted analysis of redis/redis-rb@2ba9010b91 (2026-08-23). Data as JSON: /api/errors/62e962392a2910e2. Report an issue: GitHub.