{"record":{"id":"090567a5f9f5cfac","repo":"krahets/hello-algo","slug":"error-090567","errorCode":null,"errorMessage":"индекс выходит за границы","messagePattern":"индекс выходит за границы","errorType":"exception","errorClass":"IndexError","httpStatus":null,"severity":"error","filePath":"ru/codes/ruby/chapter_array_and_linkedlist/my_list.rb","lineNumber":23,"sourceCode":"=end\n\n### Класс списка ###\nclass MyList\n  attr_reader :size       # Получить длину списка (текущее число элементов)\n  attr_reader :capacity   # Получить вместимость списка\n\n  ### Конструктор ###\n  def initialize\n    @capacity = 10\n    @size = 0\n    @extend_ratio = 2\n    @arr = Array.new(capacity)\n  end\n\n  ### Доступ к элементу ###\n  def get(index)\n    # Если индекс выходит за границы, выбрасывается исключение; далее аналогично\n    raise IndexError, \"индекс выходит за границы\" if index < 0 || index >= size\n    @arr[index]\n  end\n\n  ### Доступ к элементу ###\n  def set(index, num)\n    raise IndexError, \"индекс выходит за границы\" if index < 0 || index >= size\n    @arr[index] = num\n  end\n\n  ### Добавление элемента в конец ###\n  def add(num)\n    # При превышении вместимости по числу элементов запускается расширение\n    extend_capacity if size == capacity\n    @arr[size] = num\n\n    # Обновить число элементов\n    @size += 1\n  end","sourceCodeStart":5,"sourceCodeEnd":41,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/ru/codes/ruby/chapter_array_and_linkedlist/my_list.rb#L5-L41","documentation":"Raised by MyList#get (ru) when the requested index is outside `[0, size)`. The list is a hand-rolled dynamic array with a separate `@size` (logical length) and `@capacity`; the guard `raise IndexError, \"индекс выходит за границы\" if index < 0 || index >= size` rejects reads past the last populated slot, even if the backing array has spare capacity.","triggerScenarios":"Calling `list.get(i)` with i negative, i >= size, or after elements were removed so size shrank below a previously-valid index. Indexing with a value computed from capacity rather than size also triggers it.","commonSituations":"Using `list.capacity` instead of `list.size` as the loop bound; off-by-one in a for loop (`0..size` inclusive vs `0...size`); reading a slot after a remove shifted indices down.","solutions":["Bound lookups by `list.size`, e.g. iterate `0...list.size`.","Validate `index.between?(0, list.size - 1)` before calling get.","After remove(), remember all higher indices shift down by one."],"exampleFix":"// before\nv = list.get(i)  # raises if i >= size\n\n// after\nv = (i >= 0 && i < list.size) ? list.get(i) : nil","handlingStrategy":"validation","validationCode":"v = list.get(i) if i.between?(0, list.size - 1)","typeGuard":"def valid_get_index?(list, i) = i.between?(0, list.size - 1)","tryCatchPattern":"begin\n  v = list.get(i)\nrescue IndexError\n  v = nil\nend","preventionTips":["Bound loops with size, not capacity (0...list.size).","Recompute indices after remove() — higher slots shift down.","Validate range before get/set/insert/remove."],"tags":["ruby","dynamic-array","bounds-check","index-error"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}