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

    Ruby variables

    Scheduled Pinned Locked Moved Developers' Forum
    8 Posts 3 Posters 1.2k 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.
    • honoluludesktopH Offline
      honoluludesktop
      last edited by

      Sorry for the beginners question, but just to clarrify what I think I understand:

      1. Are all variable with the same name, in all installed plugins, the same? In other words if one plugin places a value in a variable, another plugin with a variable of the same name can use that value?
      2. Any plugin can call a procedure (not sure of what its called) in another plugin like the following one from sketchup.rb:
      
      def file_loaded?(filename)
          $loaded_files.include? filename
      end
      
      1. My copy of sketchup.rb calls:
      require 'langhandler.rb'
      $suStrings = LanguageHandler.new("gettingstarted.strings")
      

      Where is that plugin? I do not see it in my plugin folder.

      1 Reply Last reply Reply Quote 0
      • J Offline
        Jim
        last edited by

        This really deserves an in-depth answer, but I'm going to try without getting into too much jargon.

        @unknownuser said:

        1. Are all variables with the same name, in all installed plugins, the same? In other words if one plugin places a value in a variable, another plugin with a variable of the same name can use that value?

        No, but... this is true for global variables - ones that begin with $ as in $loaded_files.

        Most other variable will not clash because of the scoping rules of Ruby. It is possible and good practice to not use global variables.

        @unknownuser said:

        1. Any plugin can call a procedure (not sure of what its called) in another plugin like the following one from sketchup.rb:

        Procedures (functions in most languages) are called methods in Ruby.

        To answer your question: it really depends again on the scope in which the method was defined. file_loaded is defined in sketchup.rb. file_loaded is defined outside of any class or module and so becomes available to call from anywhere.

        A method defined inside a Class will be available to call on instances of the class only (instance methods.)

        @unknownuser said:

        Where is that plugin? I do not see it in my plugin folder.

        In addition to the Plugins folder, Sketchup has the Tools folder (right next to Plugins) for Ruby files. The sandbox tools, Instructor, Photo Grab, and Dynamic Components live there.

        The require method knows where to look for the "langhandler.rb" file by looking in the $LOAD_PATH global variable.

        And I'm not sure any of that really helped.

        Hi

        1 Reply Last reply Reply Quote 0
        • honoluludesktopH Offline
          honoluludesktop
          last edited by

          OK Jim, thanks for you help, am working my way through the SUCF Ruby tutorials now.

          <edit out> Found answer in tut. Yes, methods by the same name will cause problems.

          1 Reply Last reply Reply Quote 0
          • mitcorbM Offline
            mitcorb
            last edited by

            Honolulu:
            Are you trying to get at the possibility that different plugins may not be able to co-exist because of these variables with the same name?
            (How many times have I suspected this, when a newly downloaded plugin won't work?)

            If your response is "What the hell is he talking about?", just ignore me.

            Have a nice evening.
            mitcorb

            I take the slow, deliberate approach in my aimless wandering.

            1 Reply Last reply Reply Quote 0
            • honoluludesktopH Offline
              honoluludesktop
              last edited by

              Hi mitcorb, I am a beginner at this and Yes, sort of, attempting to move from hacking other peoples code, to writing some of my own:-)

              1 Reply Last reply Reply Quote 0
              • J Offline
                Jim
                last edited by

                @honoluludesktop said:

                OK Jim, thanks for you help, am working my way through the SUCF Ruby tutorials now.

                <edit out> Found answer in tut. Yes, methods by the same name will cause problems.

                Which is why most authors use a module as a namespace to give a unique name to their methods.

                
                #plugin1.rb
                def load(file)
                  # Bad - redefines top-level load method for everyone
                end
                
                # Better - 
                module HD
                  def self.load
                    # OK - 
                  end
                end
                
                # to use;
                HD.load(file)
                
                

                There's only a slight penalty in longer identifiers.

                There is still a possibility to select a namespace and method used by someone else, but the chance has been greatly diminished.

                Hi

                1 Reply Last reply Reply Quote 0
                • honoluludesktopH Offline
                  honoluludesktop
                  last edited by

                  Hi Jim, I was following Chris F's tut which demonstrates Modules but was unable to call certain methods:

                  
                  module
                     module.method01
                       .
                       module.module02 #call method02
                       .               #fails to return, 
                                       # depending what it contains
                     end
                     module.menthod02
                       .
                     end
                  end
                  #menu
                  
                  

                  Depending what module02 contains, it fails to return to method01 in some cases. When it fails, it seems it has something to do about where or how the methods variables are declared.

                  1 Reply Last reply Reply Quote 0
                  • honoluludesktopH Offline
                    honoluludesktop
                    last edited by

                    OK, I think I figured it out:

                    module
                       module.method01
                         .
                         data=module.module02(data) #call method02 
                         .
                       end
                       module.menthod02(data)
                         .
                         data=.....
                       end
                    end
                    #menu
                    
                    

                    I should have known that:-) Now, whats a CLASS?

                    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