Attrreporter.rb Compile Errors

Working through some of the Ruby API and still pretty green - reading through the tutorial for Attribute Reporting I copied the example code and upon loading into sketchup got the following errors:
(Note: my directory has been replaced with “…”)

----- Error Report as shown in SketchUp -----
Error Loading File attrreporter.rb
Error: …/attrreporter.rb:78: syntax error, unexpected tIDENTIFIER, expecting keyword_end
@doc_start = “”
^
…/attrreporter.rb:79: syntax error, unexpected tIDENTIFIER, expecting keyword_end
@doc_end = “”
^
…/attrreporter.rb:80: syntax error, unexpected tIDENTIFIER, expecting keyword_end
@row_start = “”
^
…/attrreporter.rb:81: syntax error, unexpected tIDENTIFIER, expecting keyword_end
@row_end = “\n”
^
…/attrreporter.rb:83: syntax error, unexpected tIDENTIFIER, expecting keyword_end
@cell_mid = “,”
^

And many more, which seem like basic errors but I’m still too new to correct. Can somebody check this code and see if I’m missing something here? It’s located on the Attribute Reporting Example in the Ruby API. Thanks very much!

Kyle

A URL to a webpage or the script ?
EDIT: N/M I found it here:
http://www.sketchup.com/intl/en/developer/docs/tutorial_attrreporting

My guess is if you are loading into newer SketchUp under Ruby 2.0, then the file needs to be encoded as UTF-8 without BOM (Bit Order Mark.) I had no load errors when the file was properly encoded. HOWEVER

This example is very old and definately needs updating.

(The edition on Google Code was archived long ago.)

Things that this old example violates:

  • Creates extension specific class at the top level binding, where only Ruby should create it’s base classes.
  • Uses extension specific global variable(s), instead of a module variable within it’s extension module.
  • Article, tells reader to save it to “Plugins” folder. We no longer just drop scripts into this toplevel folder. We create extensions per the API SketchupExtension class, and the “meat” files of the extension’s code go in a extension specific sub-folder.
    • When working on a script it is often easier to keep it in a sub-folder so you can load it with the Ruby Console using load "folder/filename.rb" so you can see the errors. Often the first error cause a mis-read of the rest of the file. So you fix them from top to bottom.
  • Various methods use slow .typename == String comparisons instead of:
    .is_a?(Sketchup::ComponentInstance), etc. which is much faster.

See: http://www.sketchup.com/intl/en/developer/docs/ourdoc/sketchupextension
AND most especially: http://extensions.sketchup.com/en/developer


Also, …
There is a “attributes.rb” script including with the Examples extension, that has been updated for later Ruby versions and properly module wrapped.
It is here:
http://extensions.sketchup.com/en/content/example-ruby-scripts

Dan -

Thanks for the excellent reply. Some of this is above my understanding but I’m sure it will become clear as I learn more. In the meantime, I’ll keep an eye on the example code - do you anticipate it being updated at some point?

Thanks!

Kyle

I do hope, sometime in the future, that Trimble budgets some resources to updating all the examples in the API docs, especially the snippets for each class and method (which are full of errors.)


Do not rely upon the SketchUp API documentation to teach you Ruby coding. That should not be it’s job, nor is it Trimble’s responsibility. There are some other topic threads in this category that discuss better places and ways to learn Ruby programming.

2 Likes