加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

如何跟踪ruby程序的执行过程

发布时间:2020-12-17 04:01:37 所属栏目:百科 来源:网络整理
导读:我是 ruby的新手,当我对某些程序感到困惑时,我想跟踪ruby程序的执行过程.我想知道是否有办法帮助我跟踪shell脚本设置-x做什么? PS: 比如shell脚本test.sh: set -xls /homeecho "hello dujun and haotianma!" 当我执行test.sh时,输出将如下: + ls /homedu
我是 ruby的新手,当我对某些程序感到困惑时,我想跟踪ruby程序的执行过程.我想知道是否有办法帮助我跟踪shell脚本设置-x做什么?

PS:

比如shell脚本test.sh:

set -x
ls /home
echo "hello dujun and haotianma!"

当我执行test.sh时,输出将如下:

+ ls /home
dujun  haotianma
+ echo 'hello dujun and haotianma!'
hello dujun and haotianma!

就像这个bash脚本在执行它之前回应每个语句一样,我想让Ruby程序显示哪些语句正在执行.

解决方法

我想你可以使用Ruby的stdlib Tracer.

我在test.rb文件中写了一个代码:

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

现在运行代码,看看幕后发生了什么:

(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)$

再看一下代码.现在我改变了跟踪点.

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

现在运行代码,看看幕后发生了什么:

(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)$

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读