Ruby In Steel 1

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

Debugging Ruby Scripts and Utilities

With Ruby In Steel

From time to time we receive enquiries asking if it is possible to debug various types of ‘external’ Ruby scripts - things such as tests, profilers and ‘utility’ scripts which don’t form an intrinsic part of the Ruby project being developed. The answer is: yes - if the script can be run by the Ruby interpreter then it can be debugged in Ruby In Steel.

The secret is to make sure that the script you want to debug forms part of a valid Ruby In Steel project and that it defines an ‘entry point’ - that is, the ‘start file’ - for the Ruby In Steel debugger. So, if the ‘main’ file of the script is called xxx.rb then the Ruby In Steel ‘start file’ must be xxx.rb.

Let’s take a very simple example of this using a Rails ‘generate’ script. Normally, in Ruby in Steel, if you wish to generate a scaffold, for example, you would use the built-in Generate panel (Ruby/Generate). Then you would select ‘scaffold’ as the script action and you would enter the name of the scaffold to be generated:

When you click ‘Go’, the Generate script will be executed as a separate program which is ‘outside’ and ‘detached from’ the Rails project which you are developing. This means that the Generate script itself is beyond the reach of the debugger. If, for some reason, you wanted to debug the Generate script, you would need to make it (temporarily) the main program so that when you start debugging, the debugger can hook into the script.

This is how to do that:

- In the Solution Explorer, select \script\generate and, in the Properties panel, add any required arguments to \Ruby\Arguments (e.g. scaffold post):

- Select Project/Project Properties.
- In the General tab set Rails/Rails Project to False.

- In the Build tab, select Ruby/Ruby Start File and browse to the script\generate file.

- Save all.
- Load script\generate and place a breakpoint, say, on line 3.

- Press F5 to debug.
- When you hit the breakpoint you can hover over variable names to see debugging information in the code editor:

- Press F11 to ‘step into’. Now use the usual debugging commands and windows such as the Watch window and Call Stack to carry on debugging the script.

When you have finished, you should, of course, undo your changes - remove the script arguments, delete the main file name from your Project Properties and set the Rails project property to True.

While this example happens to use a Generate script, the same essential technique can be used to debug other Ruby scripts too. In our example, the script was already a part of the active project. But in some cases, you may need to create (or convert/import) a project specifically to contain the script you wish to debug.

The essential thing to remember is that, in order to debug a script, you must ensure that Ruby In Steel is told to run that script. If you simply pass it for execution by the command line Ruby interpreter, the Ruby In Steel ‘Cylon’ debugger will not be able to operate on the script. Whatever the program you need to debug it must be loaded as the ‘start file’ via a Ruby In Steel project.

Bookmark and Share   Keywords:  debugger  tutorial
© SapphireSteel Software 2014