在Ruby代码中设置单个断点

14 投票
2 回答
3274 浏览
提问于 2025-04-17 07:30

我正在寻找一些Ruby代码,它的功能和下面这些Python代码差不多:

import code
code.interact(local=locals())

这些代码的主要作用是在我的代码中插入一个断点,并打开一个控制台,这样我就可以和任何变量进行互动。

有没有人知道怎么在Ruby中做到这一点?

2 个回答

1

在Ruby中,有一个叫做Kernel#local_variables的方法,它可以返回当前本地变量的名称。你可以查看相关文档了解更多信息:

ri local_variables
16

你需要使用 Pry 这个库:

require 'pry' # gem install pry
binding.pry   # Drop into the pry console

想了解更多,可以查看这里:
http://banisterfiend.wordpress.com/2011/01/27/turning-irb-on-its-head-with-pry/

另外,你也可以看看:
如何在 Sinatra 中使用 Pry?

撰写回答