自从我迁移到 Rails 7.1 和 Ruby 3 以来,我一直在努力使用 Rails 控制台历史记录。
以前,控制台保留自己的历史记录,并且调试器历史记录是分开的。因此,如果我正在调试名为 DoSomething 的服务,则调试器命令
(c)ontinue
、(n)ext
等将不会成为控制台命令历史记录的一部分。
现在我的控制台历史记录如下所示。
DoSomething.call(param)
n
n
n
n
n
c
...而且我必须按键6次才能使用
DoSomething.call(param)
。
有什么想法如何分离历史吗?
我尝试为 .rbdg_history、.irb_history 设置单独的文件名,但我不清楚我首先使用的是 IRB、Pry、Debug 和 Rbdg 中的哪一个。控制台是 IRB 吗?如果我能得到 Rails 控制台实际内容的简短摘要,我将不胜感激。
我在 Mac 上使用 rbenv。
这并不是一个完整的解决方案,至少它显示了分离两个历史所必须发生的事情。主要问题是共享的
Reline::HISTORY
在调试会话期间被修改,当您退出时,此历史记录将被写入 .rdbg_history
文件,然后因为我们也在 irb 中,所以它被保存到 .irb_history
中。
调试完成后我们必须恢复历史记录:
(此行之后)https://github.com/ruby/debug/blob/v1.9.1/lib/debug/console.rb#L200
diff --git a/lib/debug/console.rb b/lib/debug/console.rb
index b228086..a162324 100644
--- a/lib/debug/console.rb
+++ b/lib/debug/console.rb
@@ -198,6 +198,8 @@ module DEBUGGER__
end
}
}
+
+ history.slice!(@init_history_lines..-1)
end
end
end
另一个问题是退出时,代码直到 irb 保存其历史记录后才运行,但为时已晚。我发现的一种方法是手动运行它:
DEBUGGER__::SESSION.deactivate
示例:
$ rm ~/.irb_history ~/.rdbg_history
$ irb
# truncated output for clarity
>> require "debug"
>> debugger
(ruby) puts "from debug"
(rdbg) c # continue command
>> DEBUGGER__::SESSION.deactivate
>> puts "from irb"
>> exit
$ cat ~/.irb_history
require "debug"
debugger
puts "from irb"
exit
$ cat ~/.rdbg_history
puts "from debug"
c
DEBUGGER__::SESSION.deactivate