Ruby variables
-
Sorry for the beginners question, but just to clarrify what I think I understand:
- 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?
- 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
- 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.
-
This really deserves an in-depth answer, but I'm going to try without getting into too much jargon.
@unknownuser said:
- 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:
- 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.
-
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.
-
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 -
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:-)
-
@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 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.
-
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?
Advertisement