How can we call classes in different files?

Dear friends,
We have a “main_folder” and 4 different folders on it (folder1… folder4). In each folder, we have a standard plugin (plugin1…plugin4). It means in folder1 we have a folder named plugin1 and inside it a file name plugin1_data.rb. and insider this file we have a class names class1. In the same way, we have class2, class3, and class4. How can we write a plugin in main_folder and can call class1…class4 on it.
My following codes have problems.

require 'extensions.rb'
require 'sketchup.rb'
require "main_folder/folder1/plugin1/plugin1_data.rb"
require "main_folder/folder2/plugin1/plugin2_data.rb"
require "main_folder/folder3/plugin1/plugin3_data.rb"
require "main_folder/folder4/plugin1/plugin4_data.rb"
module MajidMahmoudi
  module Main
    class Main
       def activate
         @instance1 ||= MajidMahmoudi::Plugin1::Class1.new
       end
    end
    unless file_loaded?(__FILE__)
      UI.menu("Plugins").add_item("Main") {
        Sketchup.active_model.select_tool(Main.new)
      }
      file_loaded(__FILE__)
    end
  end
end #MajidMahmoudi

Also plugin1_data.rb is like this…

module MajidMahmoudi
  module Plugin1
    class Class1
      def activate
        Sketchup.active_model.active_entities.add_face [0, 0, 0], [0, 100, 0], [100, 100, 0], [100, 0, 0]
      end
    end
  end
end

Please review the SketchupExtension class. It needs each plugin (extension) to be in it’s own sub folder of the user “Plugins” folder.

This way the user can switch extensions on or off using the Extension Manager dialog.


You do not need to do this for every file or extension as these files are loaded by SketchUp just after it loads Ruby core, and before it begins to load any extensions.


The classes need only be loaded before making the call. You are calling a class that is in memory, not actually from a file.

The last statement has not problem accessing the Class1 object, but does not do anything with it.

Classes are meant to be instantiated. So you create an instance such as …

module MajidMahmoudi
  module Main
    class Main
      @instance1 ||= MajidMahmoudi::Plugin1::Class1.new

    end
  end
end

You are asking basic Ruby programming questions.
Please read some of the Ruby books that are available free.


This block is missing it’s closing end, and internally has incorrect indentation for the {} block.

Also, this is run once at load code that should be within a module, not a class.

But the “main” issue (pub intended) is that you are creating an instance of a class from within that same class itself. This would be a vicious circle. You would create the instance from outside a class definition.

1 Like

Dear Dan,
I changed codes as you advice.

For this topic sure I will consider it for main but for “plugin1_data.rb” I wish to call it from another directory and cannot have the same folder and file name. I change codes and I supposed when I run it, code in Class1 executes and I can see a face in SU. When I save files and directories in SU plugin directory codes run without any error message but no face on SU. Can you let me know what is my problem?

You are completely right after this topic I will try to be quieter and study more.
Thank you in advance,

I do not feel like guessing.

Thank you so much. You are right. @instance1.activate can solve my problem. Now I know how can I call a class in another folder.