{"record":{"id":"1c5b437464bc9c69","repo":"krahets/hello-algo","slug":"error-1c5b43","errorCode":null,"errorMessage":"佇列已滿","messagePattern":"佇列已滿","errorType":"exception","errorClass":"IndexError","httpStatus":null,"severity":"error","filePath":"zh-hant/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/zh-hant/codes/ruby/chapter_stack_and_queue/array_queue.rb#L13-L49","documentation":"Raised by the push method of the ArrayQueue teaching class (a fixed-capacity circular-array queue) when size == capacity. Unlike MyList which auto-resizes, this queue has a hard capacity ceiling set at construction time. Once the backing array is full, no more elements can be enqueued until some are popped.","triggerScenarios":"Calling queue.push(num) after capacity elements have already been enqueued without intervening pops; creating a queue with a small capacity and exceeding it; a producer pushing faster than the consumer pops.","commonSituations":"Underestimating the required queue capacity at construction; unbounded producer loops without checking is_empty/push balance; testing with a tiny capacity (e.g., 3) and hitting the ceiling immediately.","solutions":["Check 'queue.size < queue.capacity' before calling push.","Increase the capacity passed to ArrayQueue.new at construction time.","Pop/consume elements before pushing more when the queue is near capacity.","Wrap in begin/rescue IndexError to implement backpressure or retry logic."],"exampleFix":"# before\nloop { queue.push(produce_item) }  # raises once capacity is reached\n\n# after\nif queue.size < queue.capacity\n  queue.push(produce_item)\nelse\n  process(queue.pop)  # make room, then retry\n  queue.push(produce_item)\nend","handlingStrategy":"validation","validationCode":"if queue.size < queue.capacity\n  queue.push(num)\nend","typeGuard":"# Ruby: safe push\ndef safe_push(queue, num)\n  return false unless queue.size < queue.capacity\n  queue.push(num)\n  true\nend","tryCatchPattern":"begin\n  queue.push(num)\nrescue IndexError\n  # queue full: apply backpressure — pop and retry, or buffer\n  process(queue.pop)\n  retry\nend","preventionTips":["Check size < capacity before every push.","Size the queue capacity generously at construction.","This queue does NOT auto-resize — pop elements to make room."],"tags":["ruby","index-error","data-structures","queue","capacity","circular-array","fixed-size"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}