Ruby API and Ruby Classes

No.

In fact, the Extension Warehouse Developer Best Practices (Technical Requirements section) requires a SketchupExtension class instance, which makes use of at least 2 rb files and an extension sub-folder.

Yes.

Many plugins have multiple custom classes. And it helps to maintain the code if it is separated into functional portions, each portion in a separate file.

Of course you can!

However, ALL YOUR plugin’s classes MUST be wrapped within YOUR plugin’s module, and ALL YOUR various plugin modules, MUST be wrapped (defined) within YOUR toplevel namespace module.


NOTE: The examples below have now been generalized and posted in topic thread:
[Template] Multi-File, Multi-Class with SharedConstants Mixin


registrar script
.../Plugins/MRME_SomePlugin.rb
registers a SketchupExtension loader file:

# encoding: UTF-8
#
# file "MRME_SomePlugin.rb"

require 'sketchup.rb'
require 'extensions.rb'

module MRME
  module SomePlugin

    VERSION = "1.0.0"

    # Create an entry in the "Extensions" list ("Preferences" dialog):
    @@extension = SketchupExtension.new(
      "MRME: SomePlugin",                     # The extension name
      "MRME_SomePlugin/SomePlugin_loader.rb"  # The loader file path
    )
    @@extension.version = VERSION
    @@extension.creater = "MaryamR"
    @@extension.copyright = "©2016, by author, All Rights Reserved"
    @@extension.description = 'An extension that does nifty things.'
    
    # Register this extension with the Sketchup::ExtensionsManager:
    Sketchup.register_extension( @@extension, true )

  end # this plugin sub-module
end # author's toplevel namespace module

And “.../Plugins/MRME_SomePlugin/SomePlugin_loader.rb”,
might look like:

# encoding: UTF-8
#
# file "MRME_SomePlugin/SomePlugin_loader.rb"

module MRME
  module SomePlugin

    require "MRME_SomePlugin/SomePlugin_SharedConstants.rb"
    include SharedConstants # mixin the constant module

    require "MRME_SomePlugin/SomePlugin_House.rb"
    require "MRME_SomePlugin/SomePlugin_Type.rb"
    require "MRME_SomePlugin/SomePlugin_Roof.rb"
    # ... etc.

  end
end

Then the mixin module file for sharing constants:

# encoding: UTF-8
#
# file "MRME_SomePlugin/SomePlugin_SharedConstants.rb"

module MRME
  module SomePlugin
    module SharedConstants

      # define constants to be shared here:

    end
  end
end

Then of the plugin’s class definition files:

# encoding: UTF-8
#
# file "MRME_SomePlugin/SomePlugin_House.rb"

module MRME
  module SomePlugin
    class House

      include SharedConstants # mixin the constant module

      # define unshared constants here:

      # define class variables here:

      # define class methods here:

      # define instance methods here:

    end
  end
end

etc, etc, … and so forth …


The class and module blocks mean “Open the object for editing (creating the object if it does not yet exist.)”

So a Ruby class or module can be opened and edited any number of times, by a single file, or multiple files.

Tip: Ruby is a dynamic language, meaning it allows modification of classes and modules during runtime (or anytime,) not just at load time. (Or, not just at compile time like static languages.)

1 Like