{"record":{"id":"8f97340d62812744","repo":"krahets/hello-algo","slug":"error-8f9734","errorCode":null,"errorMessage":"索引越界","messagePattern":"索引越界","errorType":"exception","errorClass":"IndexError","httpStatus":null,"severity":"error","filePath":"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/codes/ruby/chapter_array_and_linkedlist/my_list.rb#L5-L41","documentation":"Ruby IndexError raised by MyList#get. The guard `raise IndexError, \"索引越界\" if index < 0 || index >= size` rejects negative indices (which Ruby's Array would otherwise wrap) and indices at/above the live element count. The custom List enforces strict bounds because its backing Array is sized to @capacity, not @size, so unchecked reads could return nil-filled capacity slots.","triggerScenarios":"Call list.get(i) with i < 0 (Ruby normally allows negative indexing, but this List forbids it), or i >= @size (pointing into the unused capacity tail, or past the last live element). Also after remove() decremented @size while a stale index is reused.","commonSituations":"Passing -1 expecting the last element (Array semantics) and hitting the negative-index rejection; loops bounded by @capacity instead of size; reusing an index captured before a remove()/clear(); off-by-one `i <= size`.","solutions":["Validate `index >= 0 && index < list.size` before get(); never bound loops on capacity.","For negative-index semantics, translate `index += list.size` first and re-check the range.","Recompute indices after any mutating operation."],"exampleFix":"# before\nv = list.get(i)\n# after\nraise ArgumentError if i < 0 || i >= list.size\nv = list.get(i)","handlingStrategy":"validation","validationCode":"# call BEFORE get(index)\nraise ArgumentError, \"bad index\" if index < 0 || index >= list.size\nv = list.get(index)","typeGuard":null,"tryCatchPattern":"begin\n  v = list.get(i)\nrescue IndexError => e\n  v = nil   # or log and skip\nend","preventionTips":["Bound indices on `size`, never on `capacity`.","Translate negative indices manually (`index += size`) if you need Array-style wrap, then re-validate.","Recompute indices after every mutating operation."],"tags":["ruby","array-list","index-out-of-bounds","data-structure"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}