But I get an error on row 7 “Undefined method `add_instance’ for ComponentDefinition”.
Is there a better way of solving my problem?
I’m using Sketchup2022.
That’s because an ComponentInstance is an Entity that is added to an Entities collection, as in your final code line, not to a ComponentDefinition. So, you would need to add the entities to the Entities collection of new_definition, not to new_definition itself.
Of course, this all begs the question of why you are trying to build a nested component in one that contains nothing else. A shell of nesting that contains only one object is a waste! What is the ultimate objective?