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] keyView on GitHub (pinned to 2ba9010b91)
Solutions
- Pass complete index/value pairs: armset(key, 0, 'a', 5, 'f') or one array per pair [ [0, 'a'], [5, 'f'] ]
- Prefer the Hash form armset(key, { 0 => 'a', 5 => 'f' }) — it cannot produce an odd list
- 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
- Build pairs with each_slice(2) or zip so odd lists are impossible
- Prefer the Hash form for sparse index writes
- Cover dynamic call sites with a test asserting complete pairs
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
- ranges must be given as start/stop pairs
- logic must be :and or :or
- value is required for the MATCH operation
- can't supply both nx and xx
- count argument must be specified
AI-assisted analysis of redis/redis-rb@2ba9010b91 (2026-08-23).
Data as JSON: /api/errors/7ea77d580319c6eb.
Report an issue: GitHub.