Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Ruby中获取顶级命名空间_Ruby_Namespaces_Diagnostics - Fatal编程技术网

在Ruby中获取顶级命名空间

在Ruby中获取顶级命名空间,ruby,namespaces,diagnostics,Ruby,Namespaces,Diagnostics,用Ruby怎么做 有时,对于解释语言中的诊断,我可以更快地对代码进行快速修改,将对象扔到顶级名称空间,然后在交互环境中处理它 在Python中,我将以下内容添加到代码中: import __main__ __main__.[field] = [my problematic object] …然后使用命令python-i[myfilename]运行该文件。知道如何在Ruby中访问顶级名称空间吗?我建议使用 运行gem install pry安装pry。然后在要启动交互式会话的位置添加以下代码 r

用Ruby怎么做

有时,对于解释语言中的诊断,我可以更快地对代码进行快速修改,将对象扔到顶级名称空间,然后在交互环境中处理它

在Python中,我将以下内容添加到代码中:

import __main__
__main__.[field] = [my problematic object]
…然后使用命令
python-i[myfilename]
运行该文件。知道如何在Ruby中访问顶级名称空间吗?

我建议使用

运行
gem install pry
安装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
交互式会话的示例

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

那么pry是调试宝石吗?(现在没有时间尝试。)很抱歉,我不太了解,但我不熟悉Ruby中的
$
符号。这是你可以简单解释的吗?Pry只是IRB的一个替代品,有更多的特性。你说的是哪一个
$
<代码>$cat debug.rb?那是贝壳。