Skip to content

Commit b850960

Browse files
committed
ファイル実行の仕様とスタックトレースを調整
1 parent 4822427 commit b850960

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

public/docs/ruby-7.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ Ruby入門
366366
* ヒント: 親クラスの `info` メソッドの結果を `super` で利用すると効率的です。
367367

368368
```ruby:practice7_2.rb
369-
require './practice7_1.rb' # 7_1のコードを実行してBookの定義を読み込みます
369+
load './practice7_1.rb' # 7_1のコードを実行してBookの定義を読み込みます
370370

371371
# ここにEBookクラスの定義を書いてください
372372

public/ruby.worker.js

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,24 @@ function flushOutput() {
9292
stderrBuffer = "";
9393
}
9494

95-
function formatRubyError(error) {
95+
function formatRubyError(error, isFile) {
9696
if (!(error instanceof Error)) {
9797
return `予期せぬエラー: ${String(error).trim()}`;
9898
}
9999

100-
return error.message;
100+
let errorMessage = error.message;
101+
102+
// Clean up Ruby error messages by filtering out internal eval lines
103+
if (errorMessage.includes("Traceback") || errorMessage.includes("Error")) {
104+
let lines = errorMessage.split("\n");
105+
lines = lines.filter((line) => line !== "-e:in 'Kernel.eval'");
106+
if (isFile) {
107+
lines = lines.filter((line) => !line.startsWith("eval:1:in"));
108+
}
109+
errorMessage = lines.join("\n");
110+
}
111+
112+
return errorMessage;
101113
}
102114

103115
async function runCode(id, payload) {
@@ -131,7 +143,7 @@ async function runCode(id, payload) {
131143

132144
rubyOutput.push({
133145
type: "error",
134-
message: formatRubyError(e),
146+
message: formatRubyError(e, false),
135147
});
136148
}
137149

@@ -168,12 +180,7 @@ async function runFile(id, payload) {
168180
}
169181

170182
// Run the specified file
171-
const fileContent = files[name];
172-
if (!fileContent) {
173-
throw new Error(`File not found: ${name}`);
174-
}
175-
176-
rubyVM.eval(fileContent);
183+
rubyVM.eval(`load ${JSON.stringify(name)}`);
177184

178185
// Flush any buffered output
179186
flushOutput();
@@ -183,7 +190,7 @@ async function runFile(id, payload) {
183190

184191
rubyOutput.push({
185192
type: "error",
186-
message: formatRubyError(e),
193+
message: formatRubyError(e, true),
187194
});
188195
}
189196

0 commit comments

Comments
 (0)