Namespace in Multiple .rb Files
-
I am creating an extension and it is time to split everything out into multiple files.
I have in my main file...
require 'sketchup' require_relative 'Model1' module MyNameSpace module MyModels def self.create_model1 ... model1 ... end unless file_loaded?(__FILE__) # add menu items menu_Models = UI.menu('Extensions').add_submenu('My Models') menu_Model1.add_item('Model 1') {self.create_model1} ... end end end
and in the second file
def model1 ... end
After reading about namespaces I included the namespace and module names in the second file, as I had seen in other extensions.
It was all working until a SketchUp restart and I got an
"Error: #<NameError: undefined local variable or method model1 for MyNameSpace::MyModels:Module>"module MyNameSpace module MyModels def model1 ... end end end
After tearing my hair out I removed the namespace and module references and after a SU restart it works again.
What have I misunderstood about namespaces and module names? If it means anything I am not using classes at all.
Also, is there a way to reset the Ruby interpreter so a SU restart is not required?
-
module MyNameSpace module MyModels def self.model1() ...
Should work ?
Then any file using that module can call the method... -
@tig said:
module MyNameSpace > > module MyModels > > def self.model1() > > ... >
Should work ?
Then any file using that module can call the method...Thanks TIG,
that worked, although I thought I had tried that - maybe without a SU restart.
On the SU forums Dan R also showed me extend self.
-
For what it's worth.
If you temporarily comment out the line: unless file_loaded?(FILE) you can run the script again without restarting SU but you will get a new plugin menu entry for every time you run it. -
@pixero said:
If you temporarily comment out the line: unless file_loaded?(FILE) you can run the script again without restarting SU but you will get a new plugin menu entry for every time you run it.
I had been loading the script from the console, then created a menu entry to do just that (from your How to Debug thread), but naively assumed it would reload any required files. Added another menu entry for the file in question and all is good...
Advertisement