Chapter 18. Debugging and Testing

image with no caption

The development of any real-world application progresses in steps. Most of us would prefer to take more steps forward than backward. To minimize the backward steps—caused by coding errors or unforeseen side effects—you can take advantage of testing and debugging techniques.

This chapter provides a brief overview of some of the most useful debugging tools available to Ruby programmers. Bear in mind, however, that if you are using a dedicated Ruby IDE, you may have more powerful visual debugging tools at your disposal. I will be discussing only the “standard” tools available to Ruby in this chapter.

IRB: Interactive Ruby

Sometimes you may just want to “try something” with Ruby. It is possible to do this using the standard Ruby interpreter: Enter ruby at the command prompt, and then enter your code one line at a time. However, this is far from being an ideal interactive environment. For one thing, the code you enter will be executed only when you enter an end-of-file character such as ^Z or ^D (that is, ctrl-Z on Windows or ctrl-D on some other operating systems). So, to do something as simple as displaying the value of 1 plus 1, you would enter the following sequence commands (remembering to enter whichever end-of-file character is required on your operating system).

ruby
1+1
^Z

Only once the end-of-file character (here ^Z) has been entered does Ruby execute the code and display the result:

2

For a better way to interact with Ruby, use the Interactive Ruby shell (IRB). To start IRB, go to a command prompt and enter the following:

irb

You should now see a prompt similar to the following:

irb(main):001:0>

Now you are ready to start entering some Ruby code. You can enter an expression over more than one line; as soon as the expression is complete, IRB will evaluate it and display the result. Try the following (pressing enter after the + on the first line):

x = ( 10 +
( 2 * 4 ) )

Finally, press enter after the closing parenthesis. Now IRB will evaluate the expression and show the result:

=> 18

You can now evaluate x. Enter this:

x

IRB shows this:

=> 18

So, up to this point, your entire IRB session should look like this:

irb(main):001:0> x = (10 +
irb(main):002:1* (2*4))
=> 18
irb(main):003:0> x
=> 18
irb(main):004:0>

Be careful, though. Try entering this:

x = (10
+ (2*4))

This time the result is as follows:

=> 8

This is, in fact, normal Ruby behavior. It is normal because a line break acts as a terminator, while the + operator, when it begins the new line, acts as a unary operator (that is, rather than adding together two values—one to its left and one to its right—it merely asserts that the single expression that follows the + is positive). You will find a fuller explanation of this in Digging Deeper in Digging Deeper.

For now, just be aware that when entering expressions one line at a time, the position of the line break is important! When using IRB, you can tell whether the interpreter considers that you have ended a statement. If you have done so, a prompt is displayed ending with > like this:

irb(main):013:1>

If a statement is complete and returns a result, a => prompt is displayed followed by the result. For example:

=> 18

If the statement is incomplete, the prompt ends with an asterisk:

irb(main):013:1*

To end an IRB session, enter the word quit or exit at the prompt. You can, if you want, load a Ruby program into IRB by passing to it the program name when you run IRB like this:

irb myprogram.rb

You may also invoke IRB with a variety of options to do things such as load a module (irb -r [load-module]) or display the IRB version number (irb -v). Many of the available IRB options are rather esoteric and are not likely to be required by most users. The full range of options may be listed by entering this at the command line:

irb --help

Although IRB may be useful for trying out some code, it does not provide all the features you need for debugging programs. Ruby does, however, provide a command-line debugger.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.158.134