Section 16.5. Measuring Code Coverage


16.4. Using irb As a Debugger

The ruby-breakpoint library is the creation of Florian Gross. This wonderful little tool enables you to set breakpoints in your code with the breakpoint method. When a breakpoint is encountered, you are thrown into an irb session. (The irb tool, interactive Ruby, is covered in more detail in Chapter 21.)

This is not part of the standard library. Install it by doing a gem install ruby-breakpoint or by any other method.

Let's take Listing 16.4 and make a couple of changes. We'll add require 'breakpoint' at the beginning and then add a call to the breakpoint method sometime after both the gets calls:

require 'breakpoint' # ... w2 = gets.chomp breakpoint # ...


Now let's run this. The following session shows how we enter irb and then we can do anything we want, including calling methods that have been previously defined or changing the values of variables.

$ ruby myprog.rb Give me a word: parental Give me another word: prenatal Executing break point at myprog.rb:23 irb(main):001:0> w1 => "parental" irb(main):002:0> w2 => "prenatal" irb(main):003:0> palindrome?(w1) => false irb(main):004:0> palindrome?("detartrated") => true irb(main):005:0> signature(w1) => "aaelnprt" irb(main):006:0> quit 'parental' is not a palindrome. 'prenatal' is not a palindrome. 'parental' and 'prenatal' are anagrams.


What is especially useful about this tool is that the code being debugged doesn't have to be command-line oriented or text based. There is a drb (distributed Ruby) client that allows remote debugging of a Ruby program running in another process.

To use this feature, you will have to issue a method call in the code to be tested (naturally before any calls to the breakpoint method):

Breakpoint.activate_drb("druby://127.0.0.1:2001", "localhost") # Start server on localhost at port 2001


Start the client with the breakpoint_client command. It will try every three seconds to contact the server until it either makes contact or you kill it.

    $ breakpoint_client druby://localhost:2001     No connection to breakpoint service at druby://localhost:2001 (DRb::DRbConnError)     Tries to connect will be made every 3 seconds...


After you connect, you still may not get an irb prompt. Execution will proceed until a breakpoint, at which time you will get a prompt.

There are more details to this library. Consult the documentation that comes with the package.




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

Similar book on Amazon

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