如何跟踪红宝石程序的执行过程 [英] How to track the execution process of ruby program
问题描述
我是红宝石的新手,当我对某些程序感到困惑时,我想跟踪红宝石程序的执行过程.我想知道是否有一种方法可以像Shell脚本set -x
一样帮助我进行跟踪?
I am new to ruby and I want to track the execution process of ruby program when I feel confused about some programs. I wonder whether there is a way to help me track just as shell scripts set -x
do?
PS:
例如shell脚本test.sh:
Such as shell script test.sh:
set -x
ls /home
echo "hello dujun and haotianma!"
当我执行test.sh时,输出将类似于:
when I execute test.sh, then the output will be like:
+ ls /home
dujun haotianma
+ echo 'hello dujun and haotianma!'
hello dujun and haotianma!
就像这个bash脚本在执行之前如何回显每个语句一样,我想让Ruby程序显示正在执行的语句.
Just as how this bash script echos each statement before executing it, I want to make a Ruby program show which statements are executing.
推荐答案
I think you can use the Ruby's stdlib Tracer
.
我在test.rb
文件中写了一个代码:
I wrote a code in my test.rb
file :
require 'tracer'
Tracer.on
class A
def square(a)
@b = a*a
result
end
def result
@b
end
end
a = A.new
puts a.square(5)
Tracer.off
现在运行代码,并查看所有内容:
Now run the code, and see all what's going on under the hood :
(arup~>Ruby)$ ruby test.rb
#0:test.rb:5::-: class A
#0:test.rb:5::C: class A
#0:test.rb:6::-: def square(a)
#0:test.rb:10::-: def result
#0:test.rb:13::E: end
#0:test.rb:15::-: a = A.new
#0:test.rb:16::-: puts a.square(5)
#0:test.rb:6:A:>: def square(a)
#0:test.rb:7:A:-: @b = a*a
#0:test.rb:8:A:-: result
#0:test.rb:10:A:>: def result
#0:test.rb:11:A:-: @b
#0:test.rb:12:A:<: end
#0:test.rb:9:A:<: end
25
#0:test.rb:18::-: Tracer.off
(arup~>Ruby)$
再次查看代码.现在,我更改了跟踪点.
Again look at the code. Now I changed trace point.
require 'tracer'
class A
def square(a)
@b = a*a
result
end
def result
@b
end
end
Tracer.on
a = A.new
puts a.square(5)
Tracer.off
现在运行代码,并查看所有内容:
Now run the code, and see all what's going on under the hood :
(arup~>Ruby)$ ruby test.rb
#0:test.rb:15::-: a = A.new
#0:test.rb:16::-: puts a.square(5)
#0:test.rb:4:A:>: def square(a)
#0:test.rb:5:A:-: @b = a*a
#0:test.rb:6:A:-: result
#0:test.rb:8:A:>: def result
#0:test.rb:9:A:-: @b
#0:test.rb:10:A:<: end
#0:test.rb:7:A:<: end
25
#0:test.rb:18::-: Tracer.off
(arup~>Ruby)$
这篇关于如何跟踪红宝石程序的执行过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!