{"record":{"id":"7121e99c0d04805f","repo":"krahets/hello-algo","slug":"error-7121e9","errorCode":null,"errorMessage":"インデックスが範囲外です","messagePattern":"インデックスが範囲外です","errorType":"exception","errorClass":"IndexError","httpStatus":null,"severity":"error","filePath":"ja/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/ja/codes/ruby/chapter_array_and_linkedlist/my_list.rb#L5-L41","documentation":"Raised by MyList#get (ja/codes/ruby/chapter_array_and_linkedlist/my_list.rb:23) when index is out of bounds (index < 0 or index >= size). MyList is the teaching re-implementation of a dynamic array; get reads @arr[index], so it guards the index explicitly like a real list would. The message is Japanese: \"Index is out of range\".","triggerScenarios":"Calling list.get(index) with index < 0 or index >= list.size. Commonly: get(list.size) (off-by-one), get(-1), or calling get after remove shortened the list without updating the caller's stored length.","commonSituations":"Off-by-one loops using <= size instead of < size; using a cached length that went stale after remove; iterating backward past 0; porting code that assumed Ruby's negative-index semantics.","solutions":["Validate 0 <= index < list.size before calling get.","Use list.size (not a stale local) as the loop bound; iterate with 0...size.","Rescue IndexError for optional access and return nil when out of range."],"exampleFix":"# before\nlist.get(list.size) # raises; one past the end\n\n# after\nlist.get(index) if index.between?(0, list.size - 1)","handlingStrategy":"validation","validationCode":"list.get(index) if index.between?(0, list.size - 1)","typeGuard":"def valid_index?(list, i); i.is_a?(Integer) && i.between?(0, list.size - 1); end","tryCatchPattern":"begin\n  list.get(index)\nrescue IndexError\n  nil\nend","preventionTips":["Always use list.size as the loop bound, never a cached length.","Iterate with 0...size (exclusive end) to avoid the off-by-one.","Remember MyList does not support negative indexing like Ruby Array."],"tags":["ruby","list","dynamic-array","precondition","indexerror","i18n-japanese"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}