Hi Jim, all,
thanks for bringing up this topic.
I might be wrong, because I'm just trying to learn Ruby and the SU Api, but I believe, it is always a good idea to encapsulate plugins into a class or a module. Else everyone potentionally stumps onto the feet of others.
Because, if you just use 'def some_method' you actually add methods to the class 'Object' beeing the parent of all other classes and thus it is easy to accidentially redefine/overwrite methods already in place.
Try the following in your ruby console, directly after starting SU (and maybe with all plugins removed, to be sure). Your input in black, output from Ruby in red:
%(black)[self.class] Object %(black)[def XXXXX; return "5 X'es"; end] nil %(black)[XXXXX] 5 X'es %(black)[self.XXXXX] 5 X'es
So, fine, we have our new method in place and it works. But now try this:
%(black)[Array.new.class] Array %(black)[Array.new.XXXXX] 5 X'es %(black)[Sketchup.active_model.XXXXX] 5 X'es %(black)[Sketchup.active_model.selection.XXXXX] 5 X'es
Huh! Were you aware of that? And was that actually your intention? Depending on the implementation of XXXXX it might even do strange things, when called on an instance of WhatEver (not in our sample, as that simply returns a string object). And that method name is unusual enough to not stump onto anybody's feet too easily.
But assume you have a need for, say, a method returning some identifier and you naturally name it 'id' ... first let's check a little bit, then define our method and recheck (your numbers will be different):
%(black)[id] 108448104 %(black)[self.id] 108448104 %(black)[Sketchup.active_model.id] 115396684 %(black)[def id; return 4711; end] nil %(black)[id] 4711 %(black)[self.id] 4711 %(black)[Sketchup.active_model.id] 4711
Nice, our method works ... but we stumped heavily onto everbodys feet. Try some other object ids you can come up with, all will give 4711. Most probably some loaded Ruby scripts are now broken too, e.g. when they test for equality of object instances with the method 'equal?'. So you better now terminate and restart SketchUp before continuing with serious work!!!
How stupid can one be to redefine/overwrite the 'id' method? OK, nobody, but if everybody just uses 'def' within the scope of the default Object, especially with several plugins already loaded, name me that man knowing all and every method name already in use in some plugin, so that I can ask him for permission to use my personal method name.
A better(?) approach would be to pre/postfix every method (still in Object) with your signature ... good, when you enjoy typing your name often, e.g. def deerwood_id ...
.
But, if you just have one to five unrelated methods to offer, then maybe a module is appropriate, e.g.
module Whatever
# a module method (note the prefix)
def Whatever.id
return 4711
end
end
and call it then as Whatever.id
returning the desired 4711 without affecting others.
But if your plugin is just a little bit more sophisticated and uses different helper methods to implement some complicated feature, then use a class. Instead of writing one several pages long method with many nested loops and if/else statements split the whole behaviour into several instance methods, that call each other.
To be continued ...
best regards, deerwood