Ruby2.0后版本的debug工具: byebug

https://github.com/deivid-rodriguez/byebug/blob/master/GUIDE.md

 

安装:

gem install byebug

 

使用:

Rails:

直接增加byebug。然后运行bin/rails s, 一旦执行到byebug这行命令,就会收到一个prompt提示。

Ruby:

如果想要debug一个ruby script无需编辑它, 直接在命令行上引用byebug命令:

byebug myscript.rb

 

使用命令

continue, next ,

step:

当=>指向一个def关键字时,使用step可以进入这个方法内部。

restart n: 

n是行号,即重新从第n行执行命令。

其他例子的使用见上面的链接guide.

 

例子:

# byebug.rb
# The n'th triangle number: triangle(n) = n*(n+1)/2 = 1 + 2 + ... + n
#
def triangle(n)
  tri = 0

  0.upto(n) { |i| tri += i }

  tri
end

t = triangle(3)
puts t

 

#进入命令行:
> byebug xxx.rb
当=>指向一个def关键字时,使用step可以进入这个方法内部。
> (byebug)step
[1, 10] in /Users/byebug.rb
    1: #
    2: # The n'th triangle number: triangle(n) = n*(n+1)/2 = 1 + 2 + ... + n
    3: #
    4: def triangle(n)
=>  5:   tri = 0
    6:
    7:   0.upto(n) { |i| tri += i }
    8:
    9:   tri
   10: end
(byebug)
(byebug) display tri
1: tri = 0

 

display tri会始终追踪tri变量。打印在命令行上。

linetrace打开路径追踪, basename设置省略path显示,finish 0 执行这行代码并把追踪结果打印出来。

 

(byebug) display tri
1: tri = 0
(byebug) set linetrace
linetrace is on
(byebug) set basename
basename is on
(byebug) finish
Tracing: byebug.rb:7   0.upto(n) { |i| tri += i }
1: tri = 0
Tracing: byebug.rb:7   0.upto(n) { |i| tri += i }
1: tri = 0
Tracing: byebug.rb:7   0.upto(n) { |i| tri += i }
1: tri = 1
Tracing: byebug.rb:7   0.upto(n) { |i| tri += i }
1: tri = 3
Tracing: byebug.rb:9   tri
1: tri = 6

 

posted @ 2019-09-05 16:03  Mr-chen  阅读(740)  评论(0编辑  收藏  举报