{"record":{"id":"8d09371475c06ca2","repo":"krahets/hello-algo","slug":"error-8d0937","errorCode":null,"errorMessage":"キューは空です","messagePattern":"キューは空です","errorType":"exception","errorClass":"IndexError","httpStatus":null,"severity":"error","filePath":"ja/codes/ruby/chapter_stack_and_queue/linkedlist_queue.rb","lineNumber":55,"sourceCode":"      @rear.next = node\n      @rear = node\n    end\n\n    @size += 1\n  end\n\n  ### デキュー ###\n  def pop\n    num = peek\n    # 先頭ノードを削除\n    @front = @front.next\n    @size -= 1\n    num\n  end\n\n  ### 先頭要素にアクセス ###\n  def peek\n    raise IndexError, 'キューは空です' if is_empty?\n\n    @front.val\n  end\n\n  ### 連結リストを Array に変換して返す ###\n  def to_array\n    queue = []\n    temp = @front\n    while temp\n      queue << temp.val\n      temp = temp.next\n    end\n    queue\n  end\nend\n\n### Driver Code ###\nif __FILE__ == $0","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/ja/codes/ruby/chapter_stack_and_queue/linkedlist_queue.rb#L37-L73","documentation":"Raised by LinkedListQueue#peek (ja) when `is_empty?` is true. Because `pop` is implemented as `num = peek; @front = @front.next; ...`, the same IndexError surfaces for BOTH `peek` and `pop` on an empty queue — the failure originates at line 55 inside peek. The guard protects against dereferencing the nil @front.","triggerScenarios":"Calling `queue.peek` or `queue.pop` on an empty LinkedListQueue. Any dequeue-after-drain, or a consumer that pops in a loop without a size check, will hit this on the first empty operation.","commonSituations":"A producer/consumer loop where the consumer outpaces the producer; popping the last item then popping once more; a driver block that peeks before enqueueing anything.","solutions":["Check `queue.is_empty?` (or `queue.size.zero?`) before calling peek or pop.","Cap your consumer loop on `queue.size > 0` rather than a fixed count.","If you need non-raising semantics, wrap pop in a helper that returns nil when empty."],"exampleFix":"// before\nqueue.pop  # raises 'キューは空です' when drained\n\n// after\nitem = queue.is_empty? ? nil : queue.pop","handlingStrategy":"validation","validationCode":"item = queue.pop unless queue.is_empty?","typeGuard":"def queue_nonempty?(q) = !q.is_empty?","tryCatchPattern":"begin\n  item = queue.pop  # also covers peek\nrescue IndexError\n  item = nil\nend","preventionTips":["Remember pop delegates to peek — guard covers both.","Bound consumer loops with `until queue.is_empty?`.","For optional dequeue, wrap pop in a helper returning nil."],"tags":["ruby","queue","linkedlist","empty-state","index-error"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}