{"record":{"id":"a660e2373a840f2d","repo":"krahets/hello-algo","slug":"error-a660e2","errorCode":null,"errorMessage":"スタックは空です","messagePattern":"スタックは空です","errorType":"exception","errorClass":"IndexError","httpStatus":null,"severity":"error","filePath":"ja/codes/ruby/chapter_stack_and_queue/array_stack.rb","lineNumber":31,"sourceCode":"\n  ### スタックの長さを取得 ###\n  def size\n    @stack.length\n  end\n\n  ### スタックが空か判定 ###\n  def is_empty?\n    @stack.empty?\n  end\n\n  ### プッシュ ###\n  def push(item)\n    @stack << item\n  end\n\n  ### ポップ ###\n  def pop\n    raise IndexError, 'スタックは空です' if is_empty?\n\n    @stack.pop\n  end\n\n  ### スタックトップ要素を参照 ###\n  def peek\n    raise IndexError, 'スタックは空です' if is_empty?\n\n    @stack.last\n  end\n\n  ### 表示用のリストを返す ###\n  def to_array\n    @stack\n  end\nend\n\n### Driver Code ###","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/ja/codes/ruby/chapter_stack_and_queue/array_stack.rb#L13-L49","documentation":"Raised by ArrayStack#pop (ja/codes/ruby/chapter_stack_and_queue/array_stack.rb:31) when the stack is empty. It calls @stack.pop; the guard rejects the operation with an explicit message rather than returning nil as stdlib Array#pop would. Message: \"スタックは空です\" (Stack is empty). Japanese mirror of error 541.","triggerScenarios":"Calling stack.pop when stack.is_empty? is true — after popping as many items as pushed, or before the first push.","commonSituations":"Unbalanced push/pop in an expression-evaluation or DFS routine; popping in a loop with the emptiness check against the wrong variable; reusing a stack across recursive calls that drain it.","solutions":["Check stack.is_empty? before pop.","Balance push and pop counts; assert before unwinding.","Rescue IndexError and return a default when empty is a valid state."],"exampleFix":"# before\nwhile true\n  stack.pop # raises once drained\nend\n\n# after\nstack.pop until stack.is_empty?","handlingStrategy":"validation","validationCode":"stack.pop unless stack.is_empty?","typeGuard":"def stack_nonempty?(s); s.respond_to?(:is_empty?) && !s.is_empty?; end","tryCatchPattern":"begin\n  stack.pop\nrescue IndexError\n  nil\nend","preventionTips":["Check is_empty? before pop.","Keep push/pop balanced in parsing/DFS routines.","Return a default when empty is a valid state."],"tags":["ruby","stack","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"}