您确实需要尝试读取help
调试器中的输出。它很好地解释了命令。
例如,作为练习,请在命令行中尝试此操作,而不是在编辑器/IDE 中:
ruby -rdebug -e 'p 1'
h
Ruby 的调试器将输出帮助摘要:
Debugger help v.-0.002b
Commands
b[reak] [file:|class:]<line|method>
b[reak] [class.]<line|method>
set breakpoint to some position
wat[ch] <expression> set watchpoint to some expression
cat[ch] (<exception>|off) set catchpoint to an exception
b[reak] list breakpoints
cat[ch] show catchpoint
del[ete][ nnn] delete some or all breakpoints
disp[lay] <expression> add expression into display expression list
undisp[lay][ nnn] delete one particular or all display expressions
c[ont] run until program ends or hit breakpoint
s[tep][ nnn] step (into methods) one line or till line nnn
n[ext][ nnn] go over one line or till line nnn
w[here] display frames
f[rame] alias for where
l[ist][ (-|nn-mm)] list program, - lists backwards
nn-mm lists given lines
up[ nn] move to higher frame
down[ nn] move to lower frame
fin[ish] return to outer frame
tr[ace] (on|off) set trace mode of current thread
tr[ace] (on|off) all set trace mode of all threads
q[uit] exit from debugger
v[ar] g[lobal] show global variables
v[ar] l[ocal] show local variables
v[ar] i[nstance] <object> show instance variables of object
v[ar] c[onst] <object> show constants of object
m[ethod] i[nstance] <obj> show methods of object
m[ethod] <class|module> show instance methods of class or module
th[read] l[ist] list all threads
th[read] c[ur[rent]] show current thread
th[read] [sw[itch]] <nnn> switch thread context to nnn
th[read] stop <nnn> stop thread nnn
th[read] resume <nnn> resume thread nnn
p expression evaluate expression and print its value
h[elp] print this help
<everything else> evaluate
开始的重要命令是s
, n
,c
和b
, 和q
。
s
进入一个方法。
n
跳过一个方法。
c number
运行(继续)直到到达 line number
。
b number
在 line 上设置断点number
。设置断点后,使用c
继续运行,直到执行该行。
q
退出调试器。
就个人而言,我使用调试器gem。其他人使用PRY,它类似于 IRB,但具有类似调试器的扩展。
知道如何使用调试器是一项很好的技能。您可以使用调试器快速追踪问题,尝试使用puts
语句将花费更长的时间,因为您可以交互地查看变量包含的内容,或者有条件地循环直到变量包含某个值。