sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    Namespace in Multiple .rb Files

    Scheduled Pinned Locked Moved Developers' Forum
    5 Posts 3 Posters 4.9k Views 3 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • B Offline
      BryanHL
      last edited by

      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?

      1 Reply Last reply Reply Quote 0
      • TIGT Offline
        TIG Moderator
        last edited by

        module MyNameSpace
        
          module MyModels
        
            def self.model1()
        
              ...
        
        

        Should work ?
        Then any file using that module can call the method...

        TIG

        1 Reply Last reply Reply Quote 0
        • B Offline
          BryanHL
          last edited by

          @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.

          1 Reply Last reply Reply Quote 0
          • PixeroP Offline
            Pixero
            last edited by

            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.

            1 Reply Last reply Reply Quote 0
            • B Offline
              BryanHL
              last edited by

              @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...

              1 Reply Last reply Reply Quote 0
              • 1 / 1
              • First post
                Last post
              Buy SketchPlus
              Buy SUbD
              Buy WrapR
              Buy eBook
              Buy Modelur
              Buy Vertex Tools
              Buy SketchCuisine
              Buy FormFonts

              Advertisement