【问题标题】:ruby rails binding.pry how to stepruby rails binding.pry 如何分步
【发布时间】:2016-06-04 17:22:18
【问题描述】:

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

这是一个例子:

def add_nums
    x = 5 + 5
    binding.pry
    x += 5
    x += 7
    return x
end  

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

【问题讨论】:

  • 您应该能够在提示符下键入help 以查看可用命令。

标签: ruby-on-rails ruby pry


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    不优雅的解决方案

    由于您可以访问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.
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2012-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 2015-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多