{"record":{"id":"f7113c134dba7338","repo":"krahets/hello-algo","slug":"error-f7113c","errorCode":null,"errorMessage":"стек пуст","messagePattern":"стек пуст","errorType":"exception","errorClass":"IndexError","httpStatus":null,"severity":"error","filePath":"ru/codes/ruby/chapter_stack_and_queue/linkedlist_stack.rb","lineNumber":41,"sourceCode":"  ### Помещение в стек ###\n  def push(val)\n    node = ListNode.new(val)\n    node.next = @peek\n    @peek = node\n    @size += 1\n  end\n\n  ### Извлечение из стека ###\n  def pop\n    num = peek\n    @peek = @peek.next\n    @size -= 1\n    num\n  end\n\n  ### Доступ к верхнему элементу стека ###\n  def peek\n    raise IndexError, 'стек пуст' if is_empty?\n\n    @peek.val\n  end\n\n  ### Преобразовать связный список в Array и вернуть ###\n  def to_array\n    arr = []\n    node = @peek\n    while node\n      arr << node.val\n      node = node.next\n    end\n    arr.reverse\n  end\nend\n\n### Driver Code ###\nif __FILE__ == $0","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/ru/codes/ruby/chapter_stack_and_queue/linkedlist_stack.rb#L23-L59","documentation":"Raised by LinkedListStack#peek (ru) on an empty stack. `pop` is implemented as `num = peek; @peek = @peek.next; @size -= 1`, so this same raise fires for both `peek` and `pop` when empty — the failure originates at the peek call inside pop. It guards against nil @peek.","triggerScenarios":"Calling `stack.peek` or `stack.pop` on an empty LinkedListStack (never pushed, or fully popped). Any pop loop that overshoots triggers it.","commonSituations":"Unwinding then over-popping; mismatched push/pop counts; an algorithm that pops-to-empty then peeks to verify.","solutions":["Guard with `stack.is_empty?` before peek or pop.","Drive pop loops with `while stack.size > 0`.","Wrap pop in a helper returning nil on empty for optional semantics."],"exampleFix":"// before\nstack.pop  # raises 'стек пуст' when empty\n\n// after\nval = stack.is_empty? ? nil : stack.pop","handlingStrategy":"validation","validationCode":"val = stack.pop unless stack.is_empty?","typeGuard":"def stack_nonempty?(s) = !s.is_empty?","tryCatchPattern":"begin\n  val = stack.pop  # also covers peek\nrescue IndexError\n  val = nil\nend","preventionTips":["pop routes through peek — guard covers both.","Drive pop loops with `while stack.size > 0`.","Linked-list stack has no capacity limit — only the empty precondition raises."],"tags":["ruby","stack","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"}