For instance, including:
- Code: Select all
trace("Entities in model: %s", Sketchup.active_model.entities.length)
in a ruby function, will display something like:
- Code: Select all
Entities in model: 1
^^^trace_sample.rb:24:in `test_trace'
Showing the ruby file name and line number where you placed the trace.
trace(format, values) can be used like puts or printf(), but you do not need to add a \n to get the carriage return. (This saves me millions of keystrokes a month - not typing in \n

Try the attached file. When you type in: load "trace_sample.rb" you should see a message like:
- Code: Select all
Entities in model: 1
^^^trace_sample.rb:24:in `test_trace'
In actual use, I usually define trace() within each class or module, so that I am not creating a global function. (Which might interfere with, be be overridden by, global functions from other developers.
e.g.
- Code: Select all
class RPS_class
def trace(*args)
... (rest of code)
end#def
end#class
Here is the source for trace if you want to grab it and use it:
(Or else get it from the attachment - which also includes a sample use of trace())
- Code: Select all
def trace(*args)
scall = caller(1)[0].to_s # who called the routine
scall2 = File::basename(scall) # only the file name itself - no path
begin
smess = sprintf(*args)
puts smess + "\n ^^^" + scall2
rescue
warn "arg ERROR in trace called from: " + caller(1)[0].to_s
end
end#def