{"record":{"id":"935c73993a4ca75c","repo":"krahets/hello-algo","slug":"error-935c73","errorCode":null,"errorMessage":"очередь пуста","messagePattern":"очередь пуста","errorType":"exception","errorClass":"IndexError","httpStatus":null,"severity":"error","filePath":"ru/codes/ruby/chapter_stack_and_queue/array_queue.rb","lineNumber":52,"sourceCode":"    # С помощью операции взятия по модулю вернуть 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    # Указатель head сдвигается на одну позицию назад; если он выходит за конец, то возвращается в начало массива\n    @front = (@front + 1) % capacity\n    @size -= 1\n    num\n  end\n\n  ### Доступ к элементу в начале очереди ###\n  def peek\n    raise IndexError, 'очередь пуста' if is_empty?\n\n    @nums[@front]\n  end\n\n  ### Вернуть список для вывода ###\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/ru/codes/ruby/chapter_stack_and_queue/array_queue.rb#L34-L70","documentation":"Raised by ArrayQueue#peek (ru) on an empty queue. `pop` delegates to peek (`num = peek`), so the SAME raise fires for both `peek` and `pop` when size is 0. The guard protects `@nums[@front]` from being read before any element exists.","triggerScenarios":"Calling `queue.peek` or `queue.pop` on an empty ArrayQueue — before any push, or after draining all elements. A consumer loop that overshoots triggers it.","commonSituations":"Consumer outpacing producer on the ring; popping the last item then popping again; a driver that peeks before the first push.","solutions":["Guard with `queue.is_empty?` before peek or pop.","Drive drain loops with `until queue.is_empty?`.","Wrap pop in a helper returning nil on empty if you want optional semantics."],"exampleFix":"// before\nqueue.pop  # raises 'очередь пуста' when empty\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":["pop routes through peek — guard covers both.","Drive consumer loops with `until queue.is_empty?`.","Pair with a capacity check on push for the bounded ring."],"tags":["ruby","queue","circular-array","empty-state","index-error"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}