Simple? How to exit execution?

In the installation process of my plugin, it would exit if some conditions do not meet. But I can not terminate the installation. I tried the below:

exit
exit 0
exit true
abort

I always got “Error: #<SystemExit: exit>”. What is the right way to stop execution?

  1. You cannot use exit in an embedded Ruby process.

  2. SketchUp installs extensions (zipped up into .RBZ archives) using: Sketchup::install_from_archiveor the end user installs the the RBZ either: using the manual “Install Extension…” button at the bottom left of the Extension Manager window, … or through the Extension Warehouse.

Read and understand how to set up a proper SketchupExtension object, with a registrar file, an extension subfolder, and code files within that subfolder.

Study how the examples are organized:

I just thinking aloud here…
(Dan, correct me please if I’m wrong :wink: )

Henry, I guess you want to achieve something like this in a registrar file:

# Author_Plugin.rb
module AuthorTopLevelNamespace
  module AuthorPlugin
    #unless file_loaded?(__FILE__)
      su_is_ok = Sketchup.version.to_i > 19
      extension_name = 'My fancy extension'
      MY_EXT = SketchupExtension.new(extension_name, 'Author_Plugin/main.rb')
      MY_EXT.description = 'Will do some really good thing'
      MY_EXT.version = '1.0'
      MY_EXT.creator = 'Author (Author at somemail dot com)'
      MY_EXT.copyright = '2021, Author'
      Sketchup.register_extension( MY_EXT, su_is_ok )
      UI.messagebox("SketchUp is too old! #{extension_name} will not load!
        Uninstall it via Extension manager!") unless su_is_ok
      MY_EXT.uncheck unless su_is_ok # may not need...?
      #file_loaded(__FILE__)
    #end
  end
end

Edit: file_loaded() removed as Dan suggested.

SketchUp Ruby extensions all run in the same environment, and exiting it would not just exit a single script from loading. Instead off using exit, you could rearrange the code with a syntax block after the if/unless statement.

# Doesn't work in SketchUp

exit if some_condition

# Stuff goes here...
# Works in SketchUp

unless some_condition
  # Stuff goes here...
end

It is not needed to push the registrar file’s path into the $loaded_files array with file_loaded().
It will just bloat the array and it’s file_loaded?() block is not needed for registrars as the load cycle only happens once.

1 Like

Oh, I guess the tutorials (on the link above) will not be updated according this in the near future …:slight_smile:
But I noted! :+1:t4:

I guess you can tell I am not a fan of using file_loaded() or file_loaded?().
It uses a global array which can have clashing strings in it. Also string comparison in Ruby is slow.

It is much faster and safer to have your extension submodule use it’s own internal boolean module variable to track it’s own load state.

1 Like