Module variable with 'require' problem
-
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 !
-
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... -
without tests...
shouldn't you require 'B' before trying to use it? i.e. at the top, or before your show method...
john
-
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
-
module M @@var=nil class M;;A include M ###########
and
module M @@var=nil class M;;B include M ###########
???
-
@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
intoObject
, which means you are including moduleM
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
-
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 ! -
@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 thatinclude
the mixin moduleM::I
If you change
@@var
value from an instance of classM::A
, all other instances will see the change. Also any modules that included theM::I
mixin will share the@@var
. (It is a proxy lookup INTO the mixin library moduleM::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.
Advertisement