如何在Byebug中不使用断点继续执行

14

使用 byebug gem 可以让我在下一个断点之前继续执行

(byebug) help

  break      -- Sets breakpoints in the source code
  catch      -- Handles exception catchpoints
  condition  -- Sets conditions on breakpoints
  continue   -- Runs until program ends, hits a breakpoint or reaches a line
  delete     -- Deletes breakpoints
  disable    -- Disables breakpoints or displays
  display    -- Evaluates expressions every time the debugger stops
  down       -- Moves to a lower frame in the stack trace
  edit       -- Edits source files
  enable     -- Enables breakpoints or displays
  finish     -- Runs the program until frame returns
  frame      -- Moves to a frame in the call stack
  help       -- Helps you using byebug
  history    -- Shows byebug's history of commands
  info       -- Shows several informations about the program being debugged
  interrupt  -- Interrupts the program
  irb        -- Starts an IRB session
  kill       -- Sends a signal to the current process
  list       -- Lists lines of source code
  method     -- Shows methods of an object, class or module
  next       -- Runs one or more lines of code
  pry        -- Starts a Pry session
  ps         -- Evaluates an expression and prettyprints & sort the result
  quit       -- Exits byebug
  restart    -- Restarts the debugged program
  save       -- Saves current byebug session to a file
  set        -- Modifies byebug settings
  show       -- Shows byebug settings
  source     -- Restores a previously saved byebug session
  step       -- Steps into blocks or methods one or more times
  thread     -- Commands to manipulate threads
  tracevar   -- Enables tracing of a global variable
  undisplay  -- Stops displaying all or some expressions when program stops
  untracevar -- Stops tracing a global variable
  up         -- Moves to a higher frame in the stack trace
  var        -- Shows variables and its values
  where      -- Displays the backtrace

我已经四处查找,但是找不到“不使用断点继续”的方法。我能想到的唯一方法是删除或注释掉byebug语句,在退出时使用q!并重新启动测试。

如何在Ruby中避免停在其他byebug语句处继续运行?

3个回答

22

自byebug 11.0.0于2019年2月15日发布以来,您可以使用continue!或别名c!

请参见此PR:https://github.com/deivid-rodriguez/byebug/pull/524

如果您正在使用Rails,则可能希望在新请求上将其重置,然后可以添加一个过滤器:

# application_controller.rb
 before_action do
    if defined?(Byebug) && Byebug.mode == :off && Rails.env.development?
      Byebug.mode = nil
    end
 end

这就是答案。c会在断点处继续并中断。c!将忽略断点继续进行。 - Daniel Viglione

6
您的代码中的byebug方法调用并不是“断点”(考虑到帮助输出中对断点的引用)。根据帮助输出,可以使用disable命令禁用breakpoint。但这并不能解决问题,因为下一个byebug会再次暂停执行。由于byebug只是一个方法调用,您可以使其具有条件性:
byebug if SomeModule.byebug?

然后,在SomeModule中,您可以使用全局变量来切换它的开/关状态。您必须在所有调用byebug的地方执行此操作,或者您可以猴子补丁(monkey-patch)byebug方法以执行相同操作,使用alias_method_chain或类似方法。 "让Byebug在不退出Pry的情况下完成执行" 是一个类似的答案。

6

我刚在代码中发现,你可以使用以下命令完全停止 byebug:

Byebug.mode = :off

由于它不会再自动连接,因此您无法重新启用它,但这很方便,例如在调试要完成运行的脚本时。


这正是我一直在寻找的。与应用控制器中的临时 before_action 配对,可以在每个请求中重新启用它。 - ardavis
有没有其他的方法可以实现它?比如--disable-all或类似的东西? - Vijay Meena

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接