Module variables and nested class
-
Hi guys !
As I said before on the forum, I'm new to ruby. I learned a lot, but despite a decent amount of research, I can't seem to figure this out :
require 'sketchup.rb' module JBB_MyModule @model = Sketchup.active_model @layers = @model.layers @customVar = #something # Some code using these @variables class JBB_LP_AppObserver < Sketchup;;AppObserver def onNewModel(newModel) #or onOpenModel @model = newModel @layers = newModel.layers @customVar = #some other thing end#def end#class end#module
It's not working. I tried local and class variables, which don't work either. It works with global variables, but obviously I'd like to avoid this. And of course it works using "Sketchup.active_model" each time, but the code becomes messy and unreadable.
What should I do ?
Thanks, jbb
-
Maybe try something like this...
require 'sketchup.rb' module JBB_MyModule class JBB_LP_AppObserver < Sketchup;;AppObserver @@model = Sketchup.active_model @@layers = @@model.layers @@customVar = #something def onNewModel(newModel) #or onOpenModel @@model = newModel @@layers = newModel.layers @@customVar = #some other thing end#def ########## # Modify # ########## if !file_loaded?(__FILE__) #then @@jpp_test = UI.menu("Plugins").add_submenu("TEST") @@jpp_test.add_item('Test') { Sketchup.active_model.select_tool JBB_MyModule;;JBB_LP_AppObserver.new } # Add toolbars jpp_test_tb = UI;;Toolbar.new "Test" jpp_test_cmd = UI;;Command.new("Test") { Sketchup.active_model.select_tool JBB_MyModule;;JBB_LP_AppObserver.new } # icons toolbar jpp_test_cmd.small_icon = "img/jpp_test_1_16.png" jpp_test_cmd.large_icon = "img/jpp_test_1_24.png" jpp_test_cmd.tooltip = "Test" jpp_test_cmd.status_bar_text = "Test" jpp_test_cmd.menu_text = "Test" jpp_test_tb = jpp_test_tb.add_item jpp_test_cmd jpp_test_tb.show end end#class end#module
-
You can use constants for this: Constants are looked up through all parent classes/modules until the first matching is found.
module JBB_MyModule self;;MODEL = Sketchup.active_model # self;; makes sure the constant is defined in this module # Otherwise we would modify a "global" MODEL constant if someone had defined one. class JBB_LP_AppObserver < Sketchup;;AppObserver def onNewModel(newModel) MODEL = newModel # or JBB_MyModule;;MODEL end#def end#class end#module
-
Thanks guys !
I used constants as Aerilius pointed, but had to use
.const_set()
for this method to work. -
Use attr, attr_reader, attr_writer, attr_accessor to create methods to access the module/class's instance variables.
The class << self syntax is needed for modules (class attributes). If you define instance attributes that is not needed.
<span class="syntaxdefault"><br /></span><span class="syntaxkeyword">require </span><span class="syntaxstring">'sketchup.rb'<br /><br /></span><span class="syntaxdefault">module JBB_MyModule<br /><br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">model </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">active_model<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">layers </span><span class="syntaxkeyword">= @</span><span class="syntaxdefault">model</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">layers<br /> </span><span class="syntaxkeyword">@</span><span class="syntaxdefault">customVar </span><span class="syntaxkeyword">= </span><span class="syntaxcomment">#something<br /><br /> </span><span class="syntaxkeyword">class << </span><span class="syntaxdefault">self<br /> attr_accessor </span><span class="syntaxkeyword">;</span><span class="syntaxdefault">model</span><span class="syntaxkeyword">, ;</span><span class="syntaxdefault">layer</span><span class="syntaxkeyword">, ;</span><span class="syntaxdefault">customVar<br /> end<br /><br /> </span><span class="syntaxcomment"># Some code using these @variables<br /><br /> </span><span class="syntaxkeyword">class </span><span class="syntaxdefault">JBB_LP_AppObserver </span><span class="syntaxkeyword">< </span><span class="syntaxdefault">Sketchup</span><span class="syntaxkeyword">;;</span><span class="syntaxdefault">AppObserver<br /><br /> def onNewModel</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">newModel</span><span class="syntaxkeyword">) </span><span class="syntaxcomment">#or onOpenModel<br /> </span><span class="syntaxdefault">JBB_MyModule</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">model </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">newModel<br /> JBB_MyModule</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">layers </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">newModel</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">layers<br /> JBB_MyModule</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">customVar </span><span class="syntaxkeyword">= </span><span class="syntaxcomment">#some other thing<br /> </span><span class="syntaxdefault">end</span><span class="syntaxcomment">#def<br /><br /> </span><span class="syntaxdefault">end</span><span class="syntaxcomment">#class<br /><br /></span><span class="syntaxdefault">end</span><span class="syntaxcomment">#module<br /> </span><span class="syntaxdefault"></span>
-
Alternatively, in the case of observers, I usually define methods in the root module which I call from the observers - defining minimum amount of code in the observer class itself.
-
Thanks a lot Thomas ! That was exactly what I was looking for.
@tt_su said:
Alternatively, in the case of observers, I usually define methods in the root module which I call from the observers - defining minimum amount of code in the observer class itself.
For what ? Less bugsplats/erros ?
-
Just because observers doesn't have access to the instance variables of the parent module. All I need observers for are tell when when things change - so I prefer to keep the logic that modifies stuff in the parent module along with the rest of it. It's an organization thing.
-
Is there any advantages or disadvantages of instance variables over the class variables in the module?
I know a few advantages for using instance vars:
- no extra @ symbol
- attr_... functions that give simpler organizations
I just wanted to know if there is any difference when it comes to using them in the module context.
-
I don't think class variables in modules has any use.
In general people (when you search the net for Ruby) will advice against class variables if you can avoid them due to that they are troublesome when you create subclasses.
-
Thank you ThomThom
Moving to instance variables
Advertisement