From: "Jesús Gabriel y Galán" Date: 2011-08-02T15:54:39+09:00 Subject: Re: system stack error in ObjectSpace.each_object On Tue, Aug 2, 2011 at 3:12 AM, luke gruber wrote: > 1) For the container idea, how would you go about doing this? In the initialize (or new) method of Foo, store self in a container (an array on Foo, or another class). But take into account that this means that all instances of Foo will have an alive reference, so they will never be garbage collected. Something like: ruby-1.8.7-p334 :003 > class FooCollection ruby-1.8.7-p334 :004?> class << self ruby-1.8.7-p334 :005?> attr_reader :collection ruby-1.8.7-p334 :006?> def << item ruby-1.8.7-p334 :007?> (@collection ||= []) << item ruby-1.8.7-p334 :008?> end ruby-1.8.7-p334 :009?> end ruby-1.8.7-p334 :010?> end => nil ruby-1.8.7-p334 :015 > class Foo ruby-1.8.7-p334 :016?> def initialize ruby-1.8.7-p334 :017?> FooCollection << self ruby-1.8.7-p334 :018?> end ruby-1.8.7-p334 :019?> end => nil ruby-1.8.7-p334 :020 > a = Foo.new => # ruby-1.8.7-p334 :021 > b = Foo.new => # ruby-1.8.7-p334 :022 > FooCollection => FooCollection ruby-1.8.7-p334 :023 > FooCollection.collection => [#, #] Take note that even if a and b go out of scope, the Foo instances they referenced will never be GCed, because they are referenced by the FooCollection.collection array. Jesus.