I’m trying to create a dynamic component with instances of other dynamic components. Could someone help me understand the error?
CaixaMDF2.rb (1.4 KB)
Cubo.rb (2.4 KB)
I’m trying to create a dynamic component with instances of other dynamic components. Could someone help me understand the error?
CaixaMDF2.rb (1.4 KB)
Cubo.rb (2.4 KB)
CaixaMDF
were called?"nome"
passed as String, then the verificaParametrosNumericos
method will return false
.add_to_model
method exist in the SketchUp Ruby API for the entities
and I did not see if you delfineden it somewhere… (it is line 20 in CaixaMDF2.rb)“Cubo.rb”
(1) Do not define custom classes at the top level ObjectSpace
. Only Ruby and the API should do this.
All of you code should be within your unique top-level namespace module. Each of your extenstions should be within a separate submodule of your namespace module.
(2) The last statement of Cubo#initialize
(ie, return instancia
) is worthless. A class’ #initialize
method ALWAYS returns the new instance of it’s class. (In this case the new instance of a Cubo
.)
return
statement would be ignored.#initialize
method gets called, it’s instance object is already created and the #initialize
method is being evaluated within this new instance object.(3) It is not normal procedure to do all the “work” within an initialize
method. Usually this is done in other methods of a class and called from the initialize
method. Generally, the initialize
method defines internal instance variables that will hold state throughout the life if the class’ instance objects.
That said, …
(4) What you show in “Cubo.rb” should not be a class as it does not have any state (properties) to hold. Really what you’ve shown is simply a module method, so Cubo
should really be module as there is nothing to instantiate. It just contains one “factory method” that producs a SketchUp API object and adds it to the model’s active entities collection.
The main error is the misuse of classes. Ie, neither Cubo
nor CaixaMDF
should be a class.
In CaixaMDF
, the calls to Cubo.new
does not return what the coder thinks it does. The Cubo
objects are basically “empty” Ruby Object
subclasses that have no state or functionality (beyond what is inherited from Object
.)
So, they cannot be later added to any entities collection using #add_instance
.
The solution is for both files to be combined into a single module with module methods.
Your answer was helpful and clarified things for me. Do you have any sample code that instantiates an object with other objects, splitting their functions? It’s quite hard to find examples like this on the web.