Ruby In Steel 1

Back to -Tutorials -Sapphire (Ruby In Steel) Tutorials -Archives -Ruby In Steel 1

Debugging Ruby with the ’Cylon’ Debugger

A guide to Ruby In Steel’s fast Ruby debugger

One of the key features of Ruby In Steel Developer is its ultra-fast ‘Cylon’ debugger. The Cylon debugger is unique to Ruby In Steel. Its key features are speed, accuracy and seamless integration with the Visual Studio environment.

With Cylon you can step through your code, watch variables, evaluate expressions and drill down into complex data structures. The Cylon debugger can be used with standalone Ruby programs or with Ruby On Rails applications.

If you have battled against slow or uninformative Ruby debuggers in other IDEs, it may be time to give Cylon a workout! In this tutorial I’ll be covering the essential features of the Cylon debugger. If you want to follow along, just download the sample debugging project (note, this has been written to test out the debugger - not to illustrate good programming style!) and go through the steps outlined below...

- Breakpoints

You can place numerous breakpoints in the files comprising your project. For the sake of simplicity we’ll just put one breakpoint in this tutorial. Find line 54 and double-click the grey margin to the far left of the editor. This will insert a breakpoint marker like this:

Press F5 to start debugging. Execution will stop on the breakpoint.

Make sure the Call Stack window and the Locals window are visible is shown (you can select these from the Debug->Windows menu).

- The Call Stack

The Call Stack provides a list of the method calls leading up to the current point of execution. That includes recursive calls to methods such as the present one, displayBookCollections().

- Locals

The Locals window displays the names and values of local variables. In the current method, there is only one local variable, index so that’s the only one that will be shown.

Note: The debugging keys mentioned in this tutorial assume you
are using the Visual C# 2005 keyboard layout. If you have some other
keyboard layout selected, the keystrokes may be different. If in doubt,
you can check the menu items to see which keys are active.

You can select a keyboard layout in the Tools->Options->Environment->Keyboard dialog.

- The Ruby Console

Make sure the interactive Ruby Console is displayed in order to show all the program output (if this is not visible, select it from View->Other Windows).

-  Step Into

The Locals window now shows that the value of index is 1. Press F11 to step into code as it executes. Here we hit a recursive call to the displayBookCollections method so we now trace back into the first line of this method (the current position is shown by the yellow arrow in the margin).

Press F11 again and we hit a call to the showAllBooks method. Press F11 once more. Now we step into the code of the showAllBooks method. Notice that the Locals window no longer displays index (as it isn’t local to this method) but it does display aBookList (which is local to the method).

- Drill-down Debugging

The aBookList variable is an array and it has a plus [+] sign to its left. Click this to ‘drill down’ and expand the array. This shows three ‘branches’ representing the individual array items. These are user defined objects and are shown to be instances of the class Book; each has a value which is the object ID assigned by Ruby. These too have [+] signs. Click these to expand them. Now you can see the types and values of each field (an instance variable such as @title) of each object in the array.

- Hover and Drill-down

In fact, you don’t need to use the Local view to see all this data, you could just hover over the name of the aBookList variable in the editor and a data tip pops up showing the values. Just as in the Locals window, you can click the [+] symbols in the data tip in order to ’drill down’ into the array to examine its internal details.

- Navigate the Call Stack

If you press F11 three more times, you will be able to step first into the showbook and then into the formatTitle methods. Notice that the Locals window shows the relevant local variables for these methods while the Call Stack window shows you the trail you have followed to get here - namely, a recursive call by the displayBookCollections method, then a call to showallbooks, then a call to showbook and finally a call to formatTitle. If you double click the method calls in the Call Stack you can ‘move back’ through these calls. The Locals window will display the local variables and their values as you do so:

- Step Out

We don’t want to trace through every single line of the formatTitle method so let’s jump right out of it. To do this press Shift+F11 to ‘step out’. This jumps to out of the current method and back to the next piece of code to be executed in the method that called it (line 41). In fact, I’ve decided I don’t really want to carry on stepping through code so let’s continue running until the next breakpoint. Press F5 to do this. And now you’ll be right back at the breakpoint.

- Step Over

Now press F11. You will now have recursed again into the displayBookCollections method. This time, let’s just step through the code of this method without tracing through any methods it may call. To do this press F10 three times to ‘step over’ lines in the current method.

- Navigating Recursive Method Calls

When the yellow arrow is on line 54 (the line with the breakpoint), pause a while. Take a look at the Call Stack window. Due to recursion, it shows three calls to the displayBookCollections method. At each turn through this method, the value of the variable, index, is incremented. We can use the Call Stack to step back through the history of these method calls. Double click each call to displayBookCollections in the Call Stack. Notice how the value of index in the Locals window changes to show its value at that particular ‘level of recursion’.

Stay paused at the breakpoint. This will give us the chance to try out a few more debugging features. The Locals window isn’t the only one that let’s you monitor the values of variables. Let’s take a look at a couple of other related windows. These windows can be activated from the Debug->Windows menu.

- Autos

The Autos window shows details of Global variables, Object Methods, Self (here, that’s the main object but will change when you are in other scopes) and any Singleton methods of Self.

- Watch Windows

The Watch windows (you may open several of these if you wish), can be used to monitor the values of selected variables of all types. You can either enter the variable names directly or you can drag and drop them from the editor. The Watch window allows easy ‘drill down’ expansion to display varying amounts of detail.

- Quick Watch Window

The Quick watch window (available on the main Debug menu) is a convenient place for trying out expressions. Here, for example, you might ask Ruby to evaluate an expression such as the following:


The result (the two arrays merged into one and reversed) can be inspected in a ‘drill down’ Quick Watch viewer. If you want to place a permanent watch on a variable or expression, click the ‘Add Watch’ button to place it into a docked Watch window.

- The Immediate Window

The Immediate Windows is useful for trying out bits of code when stopped at a breakpoint. Here you can evaluate simple expressions such as:

1 + 2

And you can enter assignments such as:

myarray  = [1,2,3,[4,5,6,[7,8,9]]]

You can also evaluate expressions such as:

newarray = myarray.reverse

The Immediate window has no convenient way of displaying complex data structures, so an array, for example, will be shown simply as [..]. However, when you evaluate local variables these will automatically be shown in the Locals window. Alternatively you can hover over a variable name in the Immediate window in order to drill down into it.

The Immediate window includes the ability to copy and paste text and you can press the up and down arrow keys to scroll back and forth through the sequence of previously entered expressions. You can also use the Ruby Console to evaluate expressions when stopped at a breakpoint and this too has a command history which can be scrolled using the arrow keys, plus the ability to hover over variables in order to evaluate them in a data tip.

You can now press F5 to finish debugging.

Other Debugging Features...

If you want to step over a specific number of lines when debugging, right click the line where you want to continue stepping and select ‘Run To Cursor’. To enable and disable selected breakpoints, use the Breakpoints Window.

- Debugging Rails

When using use the Cylon debugger to debug Rails applications, you can not only step into plain Ruby code (e.g. in a Controller) but also into embedded Ruby code (in a View).

Refer to the separate tutorial for more on Rails debugging with Cylon.

Bookmark and Share   Keywords:  tutorial

Sample code to use with this tutorial

2.5 kb
© SapphireSteel Software 2014