If you create the group too soon the garbage-collection sees it’s empty and throws it away !
If you have to make it early straight away add a temporary cpoint to it so it’s no longer empty, you can erase the cpoint after adding something else…
Otherwise make the group just before you need to add things to its entities…
There are many things in your code sample that are not known to us without seeing more, so we cannot run it to test it and must guess what they are just to analyze the problem.
For example:
groupData, modelData, model, entities arguments all came from the caller - they could in principle be anything. It seems reasonable to assume that model is the active model of the SketchUp session and that entities is some Sketchup::Entities collection, but we have no clue what groupData or modelData might be.
Geom::Transformation has no method #toSUTransformation, so we must assume that groupData.transformation returns some other kind of object that provides this method to convert to a Geom::Transformation
Not knowing what groupData is, we can’t be sure what groupData.definition returns and why you called it definitionId, though that suggests it is something other than a Sketchup::ComponentDefinition. The next point reinforces this one.
getComponentDefinition must have been defined somewhere else. It seems to imply that modelData is some object in which you gathered object info using some form of Id.
createEntity must have been defined somewhere else. That leaves the possibility that it is raising the Exception rather than the Sketchup Ruby API. In any case, we don’t know what it does.
But, all that said, my stab-in-the-dark guess is that createEntity somehow manipulates its group argument in a way that can cause the Ruby API to delete it and then a later pass of the each loop tries to pass its entities again. You could add some periodic checks of group.valid? to try to detect this.
Just a quick note that Ruby statements do not need to end in semi-colon, nor do if statements need their boolean expression wrapped within parenthesis.