This snippet works for me, and I think its fairly safe? (We'll let one of the other Ruby folk weigh in on that if they feel they need to 😄 ).
random_classes = []
ents_to_regroup = []
a = Sketchup.active_model.selection[0].explode
a.each do |e|
random_classes << e.class
end
random_classes.uniq!
random_classes.each do |e|
single_class_objects = a.grep(e)
if single_class_objects[0].respond_to?(;bounds)
then ents_to_regroup << single_class_objects
end
end
ents_to_regroup.flatten!
Sketchup.active_model.entities.add_group(ents_to_regroup)
This will take the selected component and explode it. It then looks at the class of every object that was returned and decides if that class can be added to a group by testing to see if an object from that class responds to the bounds method, (which is in the Drawingelement class and inherited by all valid SU geometry entities). So if an object from the given class does respond to the bounds method, then it is safe to try to add those objects back into a group. If they do not respond to that method, then they get skipped.
Hopefully the logic makes sense. That snippet worked fine for me. You do have to have an object selected to test that piece of code.
Chris