Reload extension without restarting SketchUp

Hi, continuing on this question. I am trying to reload my extensions without restarting SketchUp.
This is my ruby code to reload

module Extensions
def self.reload
	original_verbose = $VERBOSE
	$VERBOSE = nil
	pattern = File.join(__dir__, '**/*.rb')
	size = Dir.glob(pattern).each { |file|
		load file
		puts "loaded file " + file.to_s
	    end
        }.size
	puts "Reloaded " + size.to_s + " files."
  ensure
	$VERBOSE = original_verbose
end
end # module

In ruby console I call Extensions.reload. This makes my extensions appear twice i.e., my menu items in the extensions menu appear twice.
Note: I have tried using require instead of load but that doesn’t reload my extensions(though menu items don’t get duplicated). I have also tried adding
file paths to $LOAD_PATHS with require and load, both didn’t reload my extension.
What is the right way to reload? Am I missing something in the above script?
I am using SketchUp 2021 but I have seen the same behaviour in SketchUp 2020 as well.
CC: @eneroth3, @tt_su

To avoid this you can do e.g. something like :
(in the file where you create the menu)

module MyName
  module MyPlugin
    @@loaded = false unless defined?(@@loaded)
    
    unless @@loaded
      cmd1 = UI::Command.new("MyTool"){start_mytool_call}
      menu = UI.menu("Plugins").add_item(cmd1)
      @@loaded = true
    end
    
    def some_method
      #code
    end
    
    # other code
    
  end
end
4 Likes