Ruby API and Ruby Classes

Hello everyone,

i working on a sketchup plugin and i want to ask you, is it obligatory to have the entire program in one .rb file or is it possible to have a plugin with muliple classes ?

For example class House, class Type … and each class will contain methodes that will be used in one plugin.

so is it possible to do so ? if yes how ?

thnx in advance.

1 Like

No.

In fact, the Extension Warehouse and the SketchupExtension class requires 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

Not that it’s really a problem, but may I ask what is the connection between you and the MaryamR who posts the identical items on sketchUcation?

Some of this I covered in a prior post. Seach for “Advice to Ruby Newbies from a Not Quite Newbie Anymore”

But to elaborate on Classes:
Most sources for Ruby training assume that you are writing a stand alone application where you are defining all your own classes and loading and saving the objects you define.
But since we are writing extensions to SketchUp
many extensions are written without ever defining a new Class. Sketchup already has all the classes I usually need. (I have never created a Class except where the API UI Tool forces you to.)
And if I did create a class of objects (“house”) they would be transient because they would not be saved in .SKP files.

Also, “Class variables” (@@myvariable) aren’t just for classes but can also be used to span an entire Module and all its methods.

thank you so much for your answer, but i didn’t really understand the SharedConstants part. can you give me an example of it if it’s possible of course ?

If for example the methods in the house will need “face” as a argument so as to calculate it’s area, for the method “getarea(f)” with f is a face . How can i pass it to it ? and how can i call it in my final plugin ?

also in the MRME_Someplugin.rb file should i require all classes in it or just the loader ? can you give me an example of it’s general structure ?

thank you soo much for your answer, it really helps.

it’s my account too on sketchucation

Updated the previous post to show a dummy SketchupExtension registrar script.
(There was also a dummy example in the API docs, I linked you to.)

MAKE sure to read:

SketchUp API: SketchupExtension class

and

Developer Center: Extension Technical Requirements

I would prefer you read the old “Pick-Axe” Ruby book, and it’s chapter on Mixin Modules. I posted it in the “Ruby Resources” thread over at SketchUcation.
[doc] Programming Ruby (The “Pick-Axe” Book)

And the Ruby Core primer on Modules and Classes:
http://ruby-doc.org/core-2.0.0/doc/syntax/modules_and_classes_rdoc.html

1 Like