Advice to put class inside a ruby module

Hi,

I’m coding inside Sketchup seen 5 years now, but I learn by my self. I have still a big doubt about class and module in Sketchup ruby api. I use the Sketchup’s method of extension folder, rbz…

To isolate all my code, I create a module that I call “Adebeo_customerName_pluginName”.
After that all function are code in different file and a different module call “Adebeo_customerName_pluginName_featureName”.

After I extend the main module “…_pluginName” with all the “…_featureName”.
It’s works find but my doubt come when a create class. I create class with the name “Adebeo_customerName_className” without include,extend or anything … I don’t find this very clean because in my opinion this class should be include inside the main module “…_pluginName”.

Every time, I spend 10 minutes to test something to do it’s a fail because this things are not very clear to me.

If you have any advice, please give it to me.

Best regards.

Denis

Salut Denis!
You use good namespacing with a hierarchy of author, customer, plugin name, feature. But by using underscore inbetween, it all turns into one long module name and the module is placed at top level. So for example when you list all top-level modules in Ruby, you would get something like Array, …, Sketchup and then maybe a ten modules of your plugin.

In order to nest modules inside each other you can write like this:

module Adebeo
  module CustomerName
    module PluginName
      class FeatureName
      end
    end
  end
end

Ruby uses double colon for accessing nested modules/classes/constants. So you can access a module like Adebeo::CustomerName::PluginName. That’s similar how you wrote it with underscores above, but with the difference that now modules are nested. Adebeo is the only top level module, and the others are included inside.

When the outer modules are already defined, you can define nested modules also like this:

module Adebeo::CustomerName::PluginName
  # module content
end

When you are “inside” a module, you don’t need to write the full nested module name:

module Adebeo::CustomerName
  def self.some_method
    PluginName.some_other_method()
    PluginName::FeatureName.new()
  end
end
3 Likes

Thanks a lot for this clarification AERilius

Also see this simple example template:
[Template] Ruby Plugin main file