是否可以在 Ruby 中调试动态定义的方法?如果是这样,我该如何实现?
所以我打开了一个新的 Pry 会话并首先尝试这样:
require "byebug"
def hello(x)
byebug
x += 1
puts x
end
hello(5)
它用“*** No sourcefile available for (pry)”对我大喊。我仍然能够单步执行代码,但我看不到源代码。
然后我尝试这样做:
def hello(x)
binding.pry
x += 1
puts x
end
hello(5)
而现在情况正好相反:我可以看到源代码,但我无法单步执行代码(当我输入“step”时,它会用“NameError: undefined local variable or method `step' for主要:对象”)。看起来 Pry 实际上确实有所需的源代码,但不幸的是调试器无法工作:(
我设法使用 IPython 在 Python repl 中完成了它(虽然它在标准 Python repl 中不起作用):
def hello(x):
breakpoint()
x += 1
print(x)
hello(5)
我希望能够在 Ruby 中实现同样的目标