ruby rails binding.pry 如何一步

Bil*_*lly 5 ruby ruby-on-rails pry

使用 ruby​​ rails 'binding.pry' 时跳过一行代码的终端命令是什么?另外你知道进入、退出和继续的命令吗?

下面是一个例子:

def add_nums
    x = 5 + 5
    binding.pry
    x += 5
    x += 7
    return x
end  
Run Code Online (Sandbox Code Playgroud)

我想知道如何逐步执行此方法并在我的终端中查看 'x' 的值,直到它返回为止。谢谢

小智 8

next执行该行代码并继续执行下一行。step步骤进入一个函数。quit让程序继续运行。


小智 6

不雅的解决方案

由于您有权访问 的范围x,因此请手动输入每一行(或您想要的任何内容)并查看它如何影响您的变量。

更优雅的解决方案

查看PryDebugger (MRI 1.9.2+) 或Pry ByeBug (MRI 2+),它们为您提供手动单步执行代码的控件。如果您选择 ByeBug,简短的语法示例是:

def some_method
  puts 'Hello World' # Run 'step' in the console to move here
end

binding.pry
some_method          # Execution will stop here.
puts 'Goodbye World' # Run 'next' in the console to move here.
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助。