{"record":{"id":"407ddd78411b382f","repo":"krahets/hello-algo","slug":"error-407ddd","errorCode":null,"errorMessage":"队列已满","messagePattern":"队列已满","errorType":"exception","errorClass":"IndexError","httpStatus":null,"severity":"error","filePath":"codes/ruby/chapter_stack_and_queue/array_queue.rb","lineNumber":31,"sourceCode":"  def initialize(size)\n    @nums = Array.new(size, 0) # 用于存储队列元素的数组\n    @front = 0 # 队首指针，指向队首元素\n    @size = 0 # 队列长度\n  end\n\n  ### 获取队列的容量 ###\n  def capacity\n    @nums.length\n  end\n\n  ### 判断队列是否为空 ###\n  def is_empty?\n    size.zero?\n  end\n\n  ### 入队 ###\n  def push(num)\n    raise IndexError, '队列已满' if size == capacity\n\n    # 计算队尾指针，指向队尾索引 + 1\n    # 通过取余操作实现 rear 越过数组尾部后回到头部\n    rear = (@front + size) % capacity\n    # 将 num 添加至队尾\n    @nums[rear] = num\n    @size += 1\n  end\n\n  ### 出队 ###\n  def pop\n    num = peek\n    # 队首指针向后移动一位，若越过尾部，则返回到数组头部\n    @front = (@front + 1) % capacity\n    @size -= 1\n    num\n  end\n","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/codes/ruby/chapter_stack_and_queue/array_queue.rb#L13-L49","documentation":"Raised by `ArrayQueue#push` (array_queue.rb:31) when `size == capacity`. This queue is backed by a fixed-size array with no auto-resize; once the circular buffer is full there is no slot for the new element. Unlike a dynamic array, the capacity is set once at construction and never grows.","triggerScenarios":"Calling `queue.push(num)` after enqueuing exactly `capacity` elements without dequeuing. The rear pointer `(@front + size) % capacity` would collide with `@front`, overwriting unread data, so the guard blocks the write.","commonSituations":"Producer faster than consumer in a bounded queue; forgot to drain the queue between batches; capacity sized too small for peak load; test that enqueues N+1 into a queue sized N.","solutions":["Increase the capacity passed to `ArrayQueue.new(capacity)` to match peak enqueue depth.","Drain (pop) elements before the buffer fills — check `queue.size < queue.capacity` before push.","Switch to `LinkedListQueue` (no fixed capacity) if unbounded growth is acceptable.","Rescue IndexError and apply backpressure (skip or retry the push)."],"exampleFix":"# before\nqueue.push(item)\n\n# after\nraise IndexError, 'Queue full' if queue.size == queue.capacity\nqueue.push(item) unless queue.size == queue.capacity","handlingStrategy":"validation","validationCode":"raise IndexError, 'full' if queue.size == queue.capacity\nqueue.push(item)","typeGuard":"def queue_pushable?(queue)\n  queue.respond_to?(:capacity) && queue.respond_to?(:size) && queue.size < queue.capacity\nend","tryCatchPattern":"begin\n  queue.push(item)\nrescue IndexError\n  # backpressure: drop, retry, or enqueue later\n  false\nend","preventionTips":["Size the queue capacity to peak producer depth, not average.","Check size < capacity before push rather than relying on the raise.","Drain the queue in a consumer loop to keep it below capacity.","Consider LinkedListQueue if unbounded growth is acceptable."],"tags":["ruby","queue","capacity","bounded-buffer"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}