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_tool
only 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_tool
method 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
Tool
classes:There is NO defined
Tool
superclass.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 anil
argument 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 theTool
class callbacks. (I thinkactivate
would be the minimum.)To ask an object if it has a public
activate
method: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 < Module
returnstrue
and:
Class > Module
returnsfalse
further,
Object
is THE supreme class, the common progenitor of all other classes.
so:Class > Object
returnsfalse
andObject
is 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. Jsttrue
orfalse
I was wrong in blue words. Actually,
.methods
can be implemented in any object.134.methods, nil.methods, "dsfg".methods, false.methods, Class.methods
will always work.
Advertisement