我怎样才能在 Ruby 中做到这一点?:
有时对于解释性语言的诊断,对我的代码进行快速更改,将对象扔到顶级名称空间中,然后在交互式环境中将其弄乱,对我来说更快。
在 Python 中,我将其添加到我的代码中:
import __main__
__main__.[field] = [my problematic object]
...然后使用命令运行文件python -i [myfilename]
。知道如何访问 Ruby 中的顶级命名空间吗?
我怎样才能在 Ruby 中做到这一点?:
有时对于解释性语言的诊断,对我的代码进行快速更改,将对象扔到顶级名称空间中,然后在交互式环境中将其弄乱,对我来说更快。
在 Python 中,我将其添加到我的代码中:
import __main__
__main__.[field] = [my problematic object]
...然后使用命令运行文件python -i [myfilename]
。知道如何访问 Ruby 中的顶级命名空间吗?
我建议为此使用Pry 。
运行gem install pry
安装撬。然后在要启动交互式会话的位置添加以下代码。
require 'pry'
binding.pry
交互式会话的示例。
$ cat debug.rb
a = 7
b = 6
product = a * b
require 'pry'
binding.pry
puts "The answer is: #{product}"
$ ruby debug.rb
From: debug.rb @ line 5 in Object#N/A:
1: a = 7
2: b = 6
3: product = a * b
4: require 'pry'
=> 5: binding.pry
6: puts "The answer is: #{product}"
pry(main)> product
=> 42
pry(main)> product = -1 * a * b
=> -42
pry(main)> exit
The answer is: -42