Identify/locate the Subclass
-
During coding I came up to the point where I needed to figure whether the selected object is a sublcass of a class. I mean like
Sketchup.active_model.select_toolonly accepts subclasses, right? And it only works if the tool includes some of its methods, such as onMouseMove.I can predict how it works. Maybe it uses
@selected_tool.include?(certain_method), If method not inclduded, then the tool jst ignores the callback to current message. But with this assumption the...select_toolmethod will also accept anything, but not nil, as it describes in the API description.Is there a way to actually know whether the selected tool is actually a subclass of a class?
-
Specifically.. about SketchUp API
Toolclasses:There is NO defined
Toolsuperclass.That means that your custom tool class definition will be a subclass of
Object.Now, because of that..
Sketchup.active_model.select_tool()does NOT do any typechecking, other than to check if anilargument was passed.So.. when you pass an object to
Sketchup.active_model.select_tool()(and it's notnil,) SketchUp expects it to have the minimum of theToolclass callbacks. (I thinkactivatewould be the minimum.)To ask an object if it has a public
activatemethod:obj.respond_to?(:activate) -
About classes in general:
The logical methods can apply at the class level.
For instance, class
Class, is a subclass of classModule.so:
Class < Modulereturnstrueand:
Class > Modulereturnsfalsefurther,
Objectis THE supreme class, the common progenitor of all other classes.
so:Class > Objectreturnsfalse
andObjectis always greater than any other class.So IF you decide to set up your own tool protoclass...
Anton::Tool
and you wanted to see if a specific tool instance was a subclass:nifty_tool.is_a?(Anton::Tool)
or
nifty_tool.class < Anton::Tool... would return
true, ifnifty_tool's class was a descendant class ofAnton::Tool.If you wanted to see a list of
nifty_tool's ancestors:
puts nifty_tool.class.ancestors
-
@dan rathbun said:
obj.respond_to?(:activate)Thanks Dan, description - sure will help.
Edited: Previously, I used
obj.methods.include?(someMethod). - That will give an error if the object wouldn't be a class, nor module. But usingrespond_to?will work on any selected object without errors. JsttrueorfalseI was wrong in blue words. Actually,
.methodscan be implemented in any object.134.methods, nil.methods, "dsfg".methods, false.methods, Class.methodswill always work.
Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better π
Register LoginAdvertisement