Explode and Re-Group/Component objects imported from a 3D STEP (.stp) file

The error message …
"undefined method 'empty?' for #Sketchup::Entities:0x007ffae9daa160 (Line 36)"
means exactly what it says.
I made an incorrect assumption that children would be an array and so then respond to an #empty? method call.
It isn’t an array, and the API writers did not define an #empty? instance method for the Sketchup::Entities collection class.

So this method call needs to be changed to a more brute force method call and comparison …
children.size == 0 (instead of) children.empty?
children.size > 0 (instead of) !children.empty?

Ie …

  return true unless children && children.size > 0

Another trick is to just test the first item of an entities collection for nilness which will be nil if the collection is empty. Ie …

children[0].nil? # true if collection is empty

Or for a conditional (because in Ruby only nil and false evaluate falsely)…

  if children[0]
    # only do if collection is not empty
  end

Never mind what the example did. I haven’t really looked deep into what you are trying to do.

I was really attempting to show how to write easy to read code without the frivolous nesting of conditional blocks. (Meaning … when it is okay to shortcircuit using return and next keywords.)

You need to read the documentation for the methods before you try to use them.
The docs for all the various #add_observer methods clearly state that they take an observer object as the argument (not an entity). So these methods really “attach” an observer object (given as the passed argument) to “watch” the receiver entity.
There are also complementary #remove_observer methods to detach observers so as to stop “watching” watched entities or collections (or the application itself.)

You can only add exploded entities into another entities context if the loose entities and the new group are in the same context (entities collection.)

Well, my idea was to insert the child group into a new group before exploding the child group. The main reason is that exploding can cause primitive geometry (faces and edges) to merge with existing primitive geometry.

If there is no other primitive geometry, then perhaps something like …

parent = child.parent
material = child.material
members = child.explode
group = parent.entities.add_group(members)
group.material = material unless material.nil?

Another option might be to add a new instance of the child group with a different transformation, where you want it. Then copy materials, attributes etc. Then delete the old group.