• Login
sketchucation logo sketchucation
  • Login
🤑 SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

Module variable with 'require' problem

Scheduled Pinned Locked Moved Developers' Forum
8 Posts 4 Posters 280 Views 4 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.
  • I Offline
    inteloide
    last edited by 24 Nov 2013, 18:51

    Hello !

    I'm developping plugins that become bigger and bigger. Therefore, to make code more readable, I split it in different files, using function 'require'.
    I'm following Thomthom's golden rules 😉 , by using module and then module variable.
    But when in file linked by 'require' I cannot access to module variable anymore !

    Here is a simplified sample to explain :

    Here is the first file (called 'A.rb'):
    [pre:1oz1zcpi]

    require 'sketchup.rb'
    module M
    	class A
    		def initialize
    			@@var="123"
    		end
    		def show
    			UI.messagebox(@@var)
    			change
    			UI.messagebox(@@var)
    		end
    		require 'B.rb'
    	end
    	
    	Mod=A.new
    end #module
    
    if !file_loaded?(__FILE__) then
    	m = UI.menu("Plugins").add_item("Test")  {M;;Mod.show  }
    end
    
    file_loaded(__FILE__)
    

    [/pre:1oz1zcpi]

    And here is the second file ('B.rb'):

    
    def change
    	@@var="321"
    end
    
    

    If you run the code, you will have 2 message boxes with '123'. As change has not been called.
    Now, just replace require 'B.rb' by the code of B.rb (='def change....') and it works ! You have two mesaage boxes, one with '123' the other '321'.
    Actually, @@var is not defined anymore in B.rb ?!

    Does anyone already face this problem ? Thank you for your help !

    Humanity will do a huge step when the IT professionals will understand that computers are tools...

    1 Reply Last reply Reply Quote 0
    • T Offline
      TIG Moderator
      last edited by 24 Nov 2013, 19:30

      Initially define the...
      @@var=nil
      in the module itself, NOT inside the classes or defs... and then in each class [outside of any defs] you add...
      include moduleName
      The @@var is then available to all methods in the module and within that module's class ?
      You cannot pass @@var between them otherwise...

      TIG

      1 Reply Last reply Reply Quote 0
      • D Offline
        driven
        last edited by 24 Nov 2013, 19:35

        without tests...

        shouldn't you require 'B' before trying to use it? i.e. at the top, or before your show method...

        john

        learn from the mistakes of others, you may not live long enough to make them all yourself...

        1 Reply Last reply Reply Quote 0
        • I Offline
          inteloide
          last edited by 24 Nov 2013, 20:06

          Thank you TIG, it works !
          Driven, I didn't try your solution, but I want the B file to be part of the module, not outside.

          The full code with TIG's solution :
          Here is the first file (called 'A.rb'):

          
          require 'sketchup.rb'
          module M
          	@@var=nil
          	class A
          		include M
          		def initialize
          			@@var="123"
          		end
          		def show
          			UI.messagebox( @@var)
          			change
          			UI.messagebox( @@var)
          		end
          		require 'B.rb'
          	end
          	
          	Mod=A.new
          end #module
          
          if !file_loaded?(__FILE__) then
          	m = UI.menu("Plugins").add_item("Test")  {M;;Mod.show  }
          end
          
          file_loaded(__FILE__)
          
          

          And here is the second file ('B.rb'):

          include M
          def change
          	@@var="321"
          end
          
          

          Humanity will do a huge step when the IT professionals will understand that computers are tools...

          1 Reply Last reply Reply Quote 0
          • T Offline
            TIG Moderator
            last edited by 24 Nov 2013, 21:08

            module M
               @@var=nil
               class M;;A
                 include M
                 ###########
            

            and

            module M
               @@var=nil
               class M;;B
                 include M
                 ###########
            

            ???

            TIG

            1 Reply Last reply Reply Quote 0
            • D Offline
              Dan Rathbun
              last edited by 24 Nov 2013, 23:11

              @inteloide said:

              And here is the second file ('B.rb'):[/b]

              include M
              > def change
              > 	@@var="321"
              > end
              > 
              

              Do not do this (above.)

              You are including module M into Object, which means you are including module M in EVERYTHING!

              file "a.rb"

              module M
              
                module I
              
                  @@var = nil
              
                  def change(val="321")
                    @@var = val
                  end
              
                end # module I
              
              end # module M
              

              file "b.rb"

              module M
              
                require("a.rb")
              
                class A
                  include M;;I
                end # class
              
              end # module M
              
              

              💭

              I'm not here much anymore.

              1 Reply Last reply Reply Quote 0
              • I Offline
                inteloide
                last edited by 25 Nov 2013, 20:59

                Hi !

                Thank you Dan for your comment, you're right M was include everywhere.
                I have seen in your code that you include A.rb in B.rb, I wanted the opposite, so I tried, the code below
                For 'A.rb' :

                require 'sketchup.rb'
                module M
                	class A
                		require 'B.rb'
                		include M;;I
                		def initialize
                			@@var="123"
                		end
                		def show
                			UI.messagebox( @@var)
                			change
                			UI.messagebox( @@var)
                		end
                	end
                	
                	Mod=A.new
                end #module M
                
                if !file_loaded?(__FILE__) then
                	m = UI.menu("Plugins").add_item("Test")  {M;;Mod.show  }
                end
                
                file_loaded(__FILE__)
                

                For 'B.rb' :

                module M
                	module I
                		@@var=nil
                		def change
                			@@var="321"
                		end
                	end #module I
                end #Module M
                

                Now, the problem is that @@var must be define in B.rb : what about if I need to create a third file which require @@var ? I must declare again the same variable !
                Any solution ? (Because in my plugins, there are a lot of variables used in a lot of sub-programs...
                Any help would be apreciated !

                Humanity will do a huge step when the IT professionals will understand that computers are tools...

                1 Reply Last reply Reply Quote 0
                • D Offline
                  Dan Rathbun
                  last edited by 26 Nov 2013, 11:51

                  @inteloide said:

                  Now, the problem is that @@var must be define in B.rb : what about if I need to create a third file which require @@var ? I must declare again the same variable !

                  NO.. the @@var is a module variable. It is SHARED by ALL classes and modules that include the mixin module M::I

                  If you change @@var value from an instance of class M::A, all other instances will see the change. Also any modules that included the M::I mixin will share the @@var. (It is a proxy lookup INTO the mixin library module M::I.)

                  Files are NOT included (that is a Python feature.) IN Ruby modules are included. A file can define multiple modules.

                  @inteloide said:

                  Any solution ? (Because in my plugins, there are a lot of variables used in a lot of sub-programs...

                  If you want each class instance or sub-module to use it's OWN variables, the use instance vars, ... @var (only a single @ before the variable name.)

                  This is all basic Ruby, explained in the "Pick-Axe" Book .

                  💭

                  I'm not here much anymore.

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

                  Advertisement