Does Ruby’s GC treat a class variable (@@x) as ‘global scope’ or as
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.
@@x = 
# among other thing …
@@x.push(self) if not @@x.include?(self)
# among other things
@store = 
procObj = Proc.new do |id|
t = Test.new() @store.push(t.__id__) ObjectSpace.define_finalizer(t, procObj) return t.__id__
_id2Obj(id).kill_self() # <— retrieves Obj from id
testF = TestFactory.new()
id = testF.getATest()
I don’t want ‘t’ to be garbage collected here. GC.disable is not an
(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.