redis/redis-rb · error · ArgumentError

wrong number of arguments

Error message

wrong number of arguments

What it means

Redis#armset builds ARMSET arguments from index/value pairs. After normalizing its arguments (a single Hash is flattened, otherwise flatten(1)) it raises ArgumentError 'wrong number of arguments' when the resulting list is empty (no pairs at all) or has an odd number of elements (a value dangling without its index). This mirrors the server's arity rule, checked client-side before anything is sent.

Source

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

      # @example With an array (flat, or one array per pair)
      #   redis.armset("foo", [0, "a"], [5, "f"])
      #     # => 2
      # @example With a Hash
      #   redis.armset("foo", { 0 => "a", 5 => "f" })
      #     # => 2
      #
      # @param [String] key
      # @param [Integer, String, Array<Integer, String>, Array<Array(Integer, String)>,
      #   Hash{Integer => String}] pairs index-value pairs — as alternating
      #   index/value arguments, a single flat array, one array per pair, or a Hash
      # @return [Integer] the number of previously empty slots that were set
      def armset(key, *pairs)
        pairs = if pairs.size == 1 && pairs.first.is_a?(Hash)
          pairs.first.flatten
        else
          pairs.flatten(1)
        end
        raise ArgumentError, "wrong number of arguments" if pairs.empty? || pairs.size.odd?

        args = pairs.each_slice(2).flat_map { |index, value| [Integer(index), value] }
        send_command([:armset, key, *args])
      end

      # Get values at multiple indices in an array.
      #
      # The reply preserves the order of the requested indices and contains
      # `nil` for any index that is not set.
      #
      # @example
      #   redis.armget("foo", 0, 1, 9)
      #     # => ["a", "b", nil]
      # @example With an array of indices
      #   redis.armget("foo", [0, 1, 9])
      #     # => ["a", "b", nil]
      #
      # @param [String] key

View on GitHub (pinned to 2ba9010b91)

Solutions

  1. Pass complete index/value pairs: armset(key, 0, 'a', 5, 'f') or one array per pair [ [0, 'a'], [5, 'f'] ]
  2. Prefer the Hash form armset(key, { 0 => 'a', 5 => 'f' }) — it cannot produce an odd list
  3. Validate dynamic input before calling: flattened pairs must be non-empty and even-length

Example fix

# before
redis.armset('foo', 0, 'a', 5)  # odd pair count: ArgumentError

# after
redis.armset('foo', 0, 'a', 5, 'f')
redis.armset('foo', { 0 => 'a', 5 => 'f' })  # Hash form
Defensive patterns

Strategy: validation

Validate before calling

pairs = pairs.flatten(1)
raise ArgumentError, 'armset needs index/value pairs' if pairs.empty? || pairs.size.odd?
redis.armset(key, *pairs)

Try / catch

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

Prevention

When it happens

Trigger: redis.armset('foo') with no pairs; redis.armset('foo', 0, 'a', 5) — three elements after flatten, odd count; splatting a dynamically built array that was truncated; redis.armset('foo', {}) — the empty Hash flattens to [].

Common situations: Programmatic pair construction (each_slice/zip producing an odd-length array); data-driven writes where the last value lost its index; refactors from arset calls that drop a positional argument.

Related errors


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