Trimming group by multiple groups

How can I trim one group with multiple other groups? I figured trim would work best since it is supposed to be a nondestructive method but I can’t seem to get it to work.

group = group1
groups_to_trim = [group2,group3,group4]
groups_for_trim.map{|g| group=group.trim(g)}

You are not coding defensively. You need to ensure that the groups are manifold (watertight) and that each boolean operation returns a group and not nil.

Also the docs for the #trim method are not very clear. It is the argument group that gets trimmed.

  def trim_groups(group = Sketchup.active_model.selection[0])
    return unless group # nil if empty selection
    if group.manifold?
      model = Sketchup.active_model
      groups = model.active_entities.grep(Sketchup::Group) - [group]
      sel = model.selection
      sel.clear
      sel.add(group)
      UI.messagebox("This is the base group to Trim.",MB_OK)
      if groups.any? {|grp| grp.manifold? }
        model.start_operation("Trim Groups")
          # Iterate remaining groups
          groups.each do |trimmer|
            next puts "#{trimmer.inspect} is not manifold!" unless trimmer.manifold?
            sel.add(trimmer)
            UI.messagebox("Execute Trim Step?",MB_OK)
            result = trimmer.trim(group)
            if result
              # group is now an invalid reference
              group = result
              sel.add(group)
              puts "#{group} trimmed with #{trimmer}"
            else
              "Error trimming #{group} with #{trimmer}"
            end
          end
        model.commit_operation
        return group # The final trimmed group
      else
        puts "No manifold groups to trim with!"
      end
    else
      puts "First group is not manifold! - Trim operation cancelled."
    end
  end

Thanks!