{"record":{"id":"6bdab46f5d82eeab","repo":"krahets/hello-algo","slug":"queue-is-empty-6bdab4","errorCode":null,"errorMessage":"Queue is empty","messagePattern":"Queue is empty","errorType":"exception","errorClass":"IndexError","httpStatus":null,"severity":"error","filePath":"en/codes/ruby/chapter_stack_and_queue/array_queue.rb","lineNumber":52,"sourceCode":"    # Add num to the rear of the queue\n    rear = (@front + size) % capacity\n    # Front pointer moves one position backward\n    @nums[rear] = num\n    @size += 1\n  end\n\n  ### Dequeue ###\n  def pop\n    num = peek\n    # Move front pointer backward by one position, if it passes the tail, return to array head\n    @front = (@front + 1) % capacity\n    @size -= 1\n    num\n  end\n\n  ### Access front element ###\n  def peek\n    raise IndexError, 'Queue is empty' if is_empty?\n\n    @nums[@front]\n  end\n\n  ### Return list for printing ###\n  def to_array\n    res = Array.new(size, 0)\n    j = @front\n\n    for i in 0...size\n      res[i] = @nums[j % capacity]\n      j += 1\n    end\n\n    res\n  end\nend\n","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/en/codes/ruby/chapter_stack_and_queue/array_queue.rb#L34-L70","documentation":"Raised by ArrayQueue#peek (en/codes/ruby/chapter_stack_and_queue/array_queue.rb:52) when the queue holds zero elements. ArrayQueue is a hand-written circular-array queue from the hello-algo teaching code; peek guards the front-access precondition because reading @nums[@front] is undefined when size == 0. Because pop calls peek first (line 43), any dequeue on an empty queue surfaces the same message.","triggerScenarios":"Calling queue.peek or queue.pop when queue.size == 0. Happens on a freshly constructed ArrayQueue.new(10) before any push, or after draining every pushed element via successive pop calls (e.g. the driver's enqueue+dequeue loop overshooting count).","commonSituations":"Draining a queue in a producer/consumer loop without an emptiness check; running the driver's circular-array test loop one iteration too many; reusing a queue instance across test cases assuming it was repopulated.","solutions":["Guard the call: only peek/pop when queue.is_empty? returns false (or queue.size > 0).","Track the count of successful pushes and never pop more times than you have pushed.","Wrap the dequeue in a rescue IndexError block to supply a sentinel/nil when the queue legitimately runs dry."],"exampleFix":"# before\nqueue = ArrayQueue.new(10)\nqueue.pop # raises IndexError, \"Queue is empty\"\n\n# after\nqueue.pop unless queue.is_empty?","handlingStrategy":"validation","validationCode":"queue.pop if queue.size > 0 && !queue.is_empty?","typeGuard":"# ArrayQueue exposes size and is_empty?; no type guard needed.\ndef dequeable?(q); q.respond_to?(:is_empty?) && !q.is_empty?; end","tryCatchPattern":"begin\n  queue.pop\nrescue IndexError => e\n  nil # queue legitimately empty\nend","preventionTips":["Always call is_empty? (or check size > 0) before peek or pop.","Track the number of pushes and never pop more times than you pushed.","In producer/consumer loops, make emptiness the loop's exit condition."],"tags":["ruby","queue","circular-array","precondition","indexerror"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}