Global Hash in module (solution without $ ) ?
-
Hi.
During some googeling(think it was St.overflow) I found this solution for making an hash that has constant scope
in module. Which permits to pass the hash through classes in a module without the use of globals$.
It's appears to work pretty fine, but I then wonder if it is an acceptable method for working with Sketchup?
I.e doesent polute the plugin directory or can create havoc for other plugins.
(it would be unfortunate to trust a solution and then find out it's a big NoNo, later on..)Maybe some of you guys have similar stuff implemented already..
Here's a snippet variant of what I'm using.
module My_stuff module DOSTUFF #initialize Constants.. class Container attr_accessor ;objstorage def initialize @objstorage = {} end end OBJECTLIST = Container.new #new Hash with global scope in module. # Add something to Hash.. OBJECTLIST.objstorage[;key] = "Somevalue" # Additional code goes here...... class Myclass def initialize() @something = OBJECTLIST.objstorage[;key] end #Add stuff.... end #class end #DOSTUFF end #My_stuff
-
I don't see the difference yet from
OBJECTLIST = {}
Can you elaborate?
-
Hah, neither do I
Your solution is better, I knew some of you foxes had a simpler solution.
Can't remember what the fuzz was bout creating a whole class for a Hash..
Maybe for some mixin purposes ? (which I do not need)
In any case I'd stick to your method.Thanks
-
Or something like ?
###Usage;### AAA;;XXX.new() module AAA @@h={} @@h['xxx']='zzz' class XXX include AAA @@h['yyy']='aaa' def initialize() @@h['zzz']='ccc' @@h.sort.each{|h|puts "#{h[0]}=#{h[1]}"} end end end
-
Thanks TIG.
Your variant is probably more than I need for the moment, but still very clever.
And I presume (since nobody mentioned it already) quite safe to use ?
Advertisement