Section 16.4. Using irb As a Debugger


16.3. Using the Ruby Debugger

Truthfully, the Ruby debugger does not seem to get much use. I do not use it, and I have talked to very few who do. But it's nice to know that it's there. Here's a brief description of how it works.

Invoke the debugger simply by requiring the debug library. This can be done on the command line:

ruby -rdebug myfile.rb


At the prompt (which looks like (rdb:1) ), you can type such commands as list to list any or all of the program, step to step into a method call, and so on. A few of these commands are listed in Table 16.1 (with the abbreviation in boldface).

Table 16.1. Common Debugger Commands

Command

Description

break

List or set a breakpoint.

delete

Delete some or all breakpoints.

catch

List or set a catchpoint.

step

Step into a method.

next

Next line (step over a method).

help

Help (list all commands).

quit

Quit the debugger.


Listing 16.4 presents the code of a simple program (too simple to need debugging, really).

Listing 16.4. A Simple Program for Debugging

STDOUT.sync = true def palindrome?(word)   word == word.reverse end def signature(w)   w.split("").sort.join end def anagrams?(w1,w2)   signature(w1) == signature(w2) end print "Give me a word: " w1 = gets.chomp print "Give me another word: " w2 = gets.chomp verb = palindrome?(w1) ? "is" : "is not" puts "'#{w1}' #{verb} a palindrome." verb = palindrome?(w2) ? "is" : "is not" puts "'#{w2}' #{verb} a palindrome." verb = anagrams?(w1,w2) ? "are" : "are not" puts "'#{w1}' and '#{w2}' #{verb} anagrams."

Listing 16.5 shows an entire debugging session. Parts of it may be confusing because the console is used for standard I/O as well as for debugging.

Listing 16.5. A Simple Debugging Session

$ ruby -rdebug db.rb Debug.rb Emacs support available. db.rb:1:STDOUT.sync = true (rdb:1) b palindrome? Set breakpoint 1 at db.rb:palindrome? (rdb:1) b anagrams? Set breakpoint 2 at db.rb:anagrams? (rdb:1) b Breakpoints:   1 db.rb:palindrome?   2 db.rb:anagrams? (rdb:1) n db.rb:3:def palindrome?(word) (rdb:1) n db.rb:7:def signature(w) (rdb:1) n db.rb:11:def anagrams?(w1,w2) (rdb:1) n db.rb:15:print "Give me a word: " (rdb:1) n Give me a word: db.rb:16:w1 = gets.chomp (rdb:1) live db.rb:16:undefined local variable or method `live' for main:Object (rdb:1) n live db.rb:18:print "Give me another word: " (rdb:1) n Give me another word: db.rb:19:w2 = gets.chomp (rdb:1) n evil db.rb:21:verb = palindrome?(w1) ? "is" : "is not" (rdb:1) c Breakpoint 1, palindrome? at db.rb:palindrome? db.rb:3:def palindrome?(word) (rdb:1) n db.rb:4:  word == word.reverse (rdb:1) word "live" (rdb:1) n db.rb:22:puts "'#{w1}' #{verb} a palindrome." (rdb:1) verb "is not" (rdb:1) n 'live' is not a palindrome. db.rb:24:verb = palindrome?(w2) ? "is" : "is not" (rdb:1) n db.rb:24:verb = palindrome?(w2) ? "is" : "is not" (rdb:1) n Breakpoint 1, palindrome? at db.rb:palindrome? db.rb:3:def palindrome?(word) (rdb:1) n db.rb:4: word == word.reverse (rdb:1) c 'evil' is not a palindrome. Breakpoint 2, anagrams? at db.rb:anagrams? db.rb:11:def anagrams?(w1,w2) (rdb:1) n db.rb:12: signature(w1) == signature(w2) (rdb:1) n db.rb:28:puts "'#{w1}' and '#{w2}' #{verb} anagrams." (rdb:1) verb "are" (rdb:1) c 'live' and 'evil' are anagrams.

Be aware that if you require other libraries, you may find yourself stepping over quite a few things at the beginning. I suggest you first set a breakpoint early in your actual code and then use continue to execute up to that point.

The debugger recognizes many other commands. You can examine the call stack and move up and down in it. You can "watch" expressions and break automatically when they change. You can add expressions to a "display" list. You can handle multiple threads and switch between them.

All these features are probably not well documented anywhere. If you use the debugger, I suggest you use its online help command and proceed by trial and error.

More modern debuggers are graphical, of course. See Chapter 21, "Ruby Development Tools," for a discussion of Ruby IDEs (Integrated Development Environments) if you want this kind of tool.




The Ruby Way(c) Solutions and Techniques in Ruby Programming
The Ruby Way, Second Edition: Solutions and Techniques in Ruby Programming (2nd Edition)
ISBN: 0672328844
EAN: 2147483647
Year: 2004
Pages: 269
Authors: Hal Fulton

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net