{"record":{"id":"fe4b58d9755ad69c","repo":"redis/redis-rb","slug":"count-argument-must-be-specified","errorCode":null,"errorMessage":"count argument must be specified","messagePattern":"count argument must be specified","errorType":"exception","errorClass":"ArgumentError","httpStatus":null,"severity":"error","filePath":"lib/redis/commands/hashes.rb","lineNumber":140,"sourceCode":"      #   redis.hrandfield(\"hash\", 2)\n      #     # => [\"f1, \"f2\"]\n      # @example Get multiple random fields with values\n      #   redis.hrandfield(\"hash\", 2, with_values: true)\n      #     # => [[\"f1\", \"s1\"], [\"f2\", \"s2\"]]\n      #\n      # @param [String] key\n      # @param [Integer] count\n      # @param [Hash] options\n      #   - `:with_values => true`: include values in output\n      #\n      # @return [nil, String, Array<String>, Array<[String, Float]>]\n      #   - when `key` does not exist, `nil`\n      #   - when `count` is not specified, a field name\n      #   - when `count` is specified and `:with_values` is not specified, an array of field names\n      #   - when `:with_values` is specified, an array with `[field, value]` pairs\n      def hrandfield(key, count = nil, withvalues: false, with_values: withvalues)\n        if with_values && count.nil?\n          raise ArgumentError, \"count argument must be specified\"\n        end\n\n        args = [:hrandfield, key]\n        args << count if count\n        args << \"WITHVALUES\" if with_values\n\n        parser = Pairify if with_values\n        send_command(args, &parser)\n      end\n\n      # Delete one or more hash fields.\n      #\n      # @param [String] key\n      # @param [String, Array<String>] field\n      # @return [Integer] the number of fields that were removed from the hash\n      def hdel(key, *fields)\n        fields.flatten!(1)\n        send_command([:hdel, key].concat(fields))","sourceCodeStart":122,"sourceCodeEnd":158,"githubUrl":"https://github.com/redis/redis-rb/blob/2ba9010b91dab9e0fde1fbae3a9aae003f8bc307/lib/redis/commands/hashes.rb#L122-L158","documentation":"hrandfield mirrors the server syntax HRANDFIELD key [count [WITHVALUES]]: WITHVALUES only exists on the count form, because with count omitted the command returns a single field and pairing values makes no sense. Passing with_values: true (or the legacy withvalues: alias) while count is nil raises ArgumentError 'count argument must be specified' before anything is sent.","triggerScenarios":"redis.hrandfield('hash', with_values: true); redis.hrandfield('hash', withvalues: true); count passed conditionally so it ends up nil while with_values is set.","commonSituations":"Porting from CLI/HRANDFIELD usage; method signatures where count is optional but with_values was defaulted on; older code using the withvalues: spelling.","solutions":["Pass a count: redis.hrandfield('hash', 5, with_values: true) returns [[field, value], ...] pairs","For a single random field, drop with_values entirely: redis.hrandfield('hash')","When count is dynamic, default it: with_values ? (count || 1) : count"],"exampleFix":"# before\nredis.hrandfield('hash', with_values: true)  # ArgumentError\n\n# after\nredis.hrandfield('hash', 5, with_values: true)  # => [[\"f1\", \"v1\"], ...]","handlingStrategy":"validation","validationCode":"count ||= 1 if with_values\nredis.hrandfield(key, count, with_values: with_values)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Treat with_values as meaningful only together with a count","Default optional counts before the call when with_values may be set","Note withvalues: is a legacy alias — settle on with_values:"],"tags":["argument-validation","hashes","random-sampling"],"backgroundTag":"missing-required-argument","analyzedSha":"2ba9010b91dab9e0fde1fbae3a9aae003f8bc307","analyzedAt":"2026-08-23T03:54:57.017Z","schemaVersion":2},"datasetVersion":"2026-08-23T08:06:27.607Z"}