From: Madan Manoharan Date: 2006-05-19T22:58:28+09:00 Subject: Question on GC Hello Rubyists: Does Ruby's GC treat a class variable (@@x) as 'global scope' or as 'local scope'? Here is my situation: I have a class variable that holds instances of 'self'. These objects are not referenced any where else; these objects are retrieved using its __id__ which is stored else where. It is my understanding that Ruby's GC doesn't see these stored __id__ as a reference to the objects (since __id__ -> Fixnum). I don't want these objects to be garbage collected as long as its instance is referred the class variable. Please note that the objects are not referenced by other class's objects. Example: class Test @@x = [] def initialize() # among other thing ... @@x.push(self) if not @@x.include?(self) end def kill_self() # among other things @@x.delete(self) end end class TestFactory @store = [] def getATest() procObj = Proc.new do |id| @store.delete(id) end t = Test.new() @store.push(t.__id__) ObjectSpace.define_finalizer(t, procObj) return t.__id__ end def releaseTest(id) _id2Obj(id).kill_self() # <--- retrieves Obj from __id__ end end testF = TestFactory.new() id = testF.getATest() ....... ....... # I don't want 't' to be garbage collected here. GC.disable is not an option ....... ....... testF.releaseTest(id) __END__ Questions: (1) Will 't' be garbage collected since the only reference to 't' is in @@x, which could go out of scope? (2) Is my understanding 'scope' correct? Any help will be greatly appreciated. Thanks, -Madan.