Ruby调试

为什么80%的码农都做不了架构师?>>>   hot3.png

转自:http://www.yiibai.com/ruby/ruby_debugger.html

一种语言多么容易使用,它通常包含了一些错误,如果它是多长了几行。为了帮助交易与错误,Ruby的标准分配包括一个调试.

为了启动Ruby调试器,加载调试库使用的命令行选项-r调试。调试器之前停止的可执行代码的第一行,并要求输入用户命令.

使用语法:

下面是使用Ruby调试器的使用语法:

$ ruby -r debug filename[, ...]

Ruby调试器命令:

下面是调试程序时,您可以使用完整的命令列表。这是没有必要使用完整的关键字给命令的一部分,在给[...]选项.

SN Command with Description
1 b[reak] [< file| class>:]< line| method>
Sets breakpoint to some position. Breakpoint is a place where you want to pause program execution for debugging purpose.
2 wat[ch] expression
Sets watchpoints
3 cat[ch] (exception|off)
Sets catchpoint to an exception.
4 b[reak]
Displays breakpoints and watchpoints
5 del[ete] [n]
Deletes breakpoints
6 disp[lay] expression
Displays value of expression
7 undisp[lay] [ n]
Removes display of n
8 c[ont]
Continues execution
9 s[tep] [ n]
Executes next n lines stepping into methods
10 n[ext] [ n]
Executes next n lines stepping over methods
11 w[here]
Displays stack frame
12 f[rame]
Synonym for where
13 l[ist][<-| n- m>]
Displays source lines from n to m
14 up [ n]
Moves up n levels in the stack frame
15 down [ n]
Moves down n levels in the stack frame
16 fin[ish]
Finishes execution of the current method
17 tr[ace] [on|off]
Toggles trace mode on and off
18 q[uit]
Exits debugger
19 v[ar] g[lobal]
Displays global variables
20 v[ar] l[ocal]
Displays local variables
21 v[ar] i[instance] object
Displays instance variables of object
22 v[ar] c[onst] object
Displays constants of object
23 m[ethod] i[instance] object
Displays instance methods of object
24 m[ethod] class| module
Displays instance methods of the class or module
25 th[read] l[ist]
Displays threads
26 th[read] c[ur[rent]]
Displays current thread
27 th[read] n
Stops specified thread
28 th[read] stop >
Synonym for th[read] n
29 th[read] c[ur[rent]] n>
Synonym for th[read] n
30 th[read] resume >
Resumes thread n
31 expression
Evaluates the expression
32 h[elp]
Displays help message
33 everything else
Evaluates.

例子:

考虑下列文件hello.rb需要调试:

#!/usr/bin/env ruby
class Hello
   def initialize( hello )
      @hello  = hello
   end
   def hello
      @hello 
   end
end

salute = Hello.new( "Hello, Mac! - by www.yiibai.com" )
puts salute.hello

这里是一个交互式会话捕获。大胆写在给定的命令:

[root@ruby]# ruby -r debug hello.rb Debug.rb
Emacs support available.

hello.rb:3:class Hello
(rdb:1) v l salute => nil
(rdb:1) b 10 Set breakpoint 1 at hello.rb:10
(rdb:1) c Hello, Mac!
[root@ruby]#

转载于:https://my.oschina.net/ruby65/blog/72945

你可能感兴趣的:(ruby)