$ versus @
-
I'd suggest always re-instance a Tool unless there is a compelling reason to make it persist.
As a general rule, releasing resources as soon as possible is a GoodThing, and secondly - and probably more importantly in a Ruby context, to ensure you don't carry references to Ruby objects and therefore stop garbage collection happening - worse still carry stale references to objects that have subsequently been deleted which tends to make SU jump into the azure blue sea of unallocated heap store and commit hare-kiri.
Adam
-
@didier bur said:
Hi,
But it is a lazy way of retaining values for your dialog boxes for instance, so user get the last values used.
One can avoid globals when using classes and methods, classes variables (@@) and objects variables (@).I'm sorry but I cant let this topic die just yet. I have carefully reread all the
comments. Does this quote offer a solution to retaining values in Dialog Boxes? -
For a dialog's default values that you want keeping from session to session within a particular model, I write them as attributes to the model itself: when the dialog initialises it looks for their values, if they are not there it takes defaults - otherwise you have them saved on a model by model basis... For an example see my TextTag.rb.
.
-
So, there you have it - three solutions that avoid globals:
- Sketchup.read_default and Sketchup.write_default (persistent across SketchUp sessions and models)
- Attributes (persistent within a given model, between sessions)
- Class variables (@@variable) (persistent only within a SketchUp session)
-
For persistent data for Dialog Boxes, you should use Sketchup.write_default and Sketchup.read_default. The keys and values are stored in the registry (Windows) and the plist (Mac).
Todd
-
@rickw said:
So, there you have it - three solutions that avoid globals:
- Sketchup.read_default and Sketchup.write_default (persistent across SketchUp sessions and models)
- Attributes (persistent within a given model, between sessions)
- Class variables (@@variable) (persistent only within a SketchUp session)
Thanks everyone; I took a quick look at TIG's, TextTag.rb. It appears to take a little bit more understanding of Ruby then just simply banging out a mass replacement of @ to $ or vise versa.
Nevertheless I will try to implement
these attributes on an exisitng Ruby of mine. -
Hi all !
Just one question : How can you do when you have that :
class ToolsObsTest < Sketchup;;ToolsObserver def onActiveToolChanged (tools_object, toolname, toolid) [b]@t[/b] = toolid end endand this variable @t is used here :
module def [b]@t[/b] end endSo the variable is not used inside the first class section, but inside the instance of a module section ???
To find variable, I use $....How can I preserve variable value ????
-
Just define your Observer class in the scope of your Module
module Foo class ToolsObsTest < Sketchup;;ToolsObserver def onActiveToolChanged (tools_object, toolname, toolid) @t = toolid end end end instance = Foo;;ToolsObsTest.new -
Hi AdamB !
Thak you for your answer...I tried it but it didn't work for me... @toolID (variable name in the code) always returns nil....
here is the real "tree" of the code (including your advice)module Toto class ToolsObsTest < Sketchup;;ToolsObserver def onActiveToolChanged (tools_object, toolname, toolid) @toolID = toolid end end ### def self.act model.tools.add_observer(Toto;;ToolsObsTest.new) end ### def self.obs(id) @toolID end end
Do you know why your method doesn't work ?
Thank you !
-
Matt,
The two @toolid are different. One is a Class instance variable, the other a Module variable.
If you want to track the toolid in module Toto, then use a method to set its value, which you can call from the class.
Note that normally, you might use a Module variable, with @@, (since module instance variables do not really have real application)def Toto.set_toolid(toolid) @@toolid = toolid end def Toto.get_toolid() @@toolid endFredo
-
Hello Fredo6 !
Thank you for your answer ! It works great ! get_toolid & set_toolid are perfect !
Just one thing, @@variable doesn't work. Just @variable...Thank you Fredo !

Hello! It looks like you're interested in this conversation, but you don't have an account yet.
Getting fed up of having to scroll through the same posts each visit? When you register for an account, you'll always come back to exactly where you were before, and choose to be notified of new replies (either via email, or push notification). You'll also be able to save bookmarks and upvote posts to show your appreciation to other community members.
With your input, this post could be even better π
Register LoginAdvertisement