Debugging Scripts

Debugging Messsages

If you include code like:
printf("entity type is: %s\n",entity.class)
in your ruby scripts, then is will output messages to the ruby console.


By adding traceback information to messages, you can also display where the messages originated in your ruby code.

Using the Ruby caller() function, you can get information on who called a routine - either the immediate caller, or the full traceback.
  • caller(1)[0] - returns the callback information for the immediate, preceding caller.

This is a sample ruby file defining a routine trace(*args).
  • trace() is used much like sprintf, except that it automatically adds information to display where in your code the message was displayed.

def test_trace
        trace("4 + 1 = %s", 4+1)

# display a message with a trace back of where it originated
def trace(*args)
        scall = caller(1)[0].to_s # who called the routine

                smess = sprintf(*args)
                warn "arg ERROR in trace called from: " + caller(1)[0].to_s

        printf("%s\n\t(%s)\n", smess, scall)


Here test_trace.rb was loaded into the ruby console (user entries are in bold).
Notice how the source file and line number are displayed along with the trace information.
load '/tmp/test_trace.rb'
4 + 1 = 5
    (/tmp/test_trace.rb:3:in `test_trace')


Error Messages

If there is an error in your Ruby Script, an error message will be displayed in the Ruby Console.

See Also

What are Ruby Scripts and Plugins? 

Editing History