On 8/27/06, Jez Stephens <jezstephens / gmail.com> wrote:
> I want to be able to keep track of all instances of a class using
> something simple like this:
>
> Class Thing
>     @@allthings = []
>
>     def initialize
>         @@allthings.push self
>     end
> end
>
> However an obvious drawback to this approach is that instances of this
> class will never be garbage collected due to the reference held in
> @@allthings.
>
> Is there a way to make it so this reference in @@allthings "doesn't
> count", so to speak?
>
> Or is there a better way of doing it?

ObjectSpace already does it for you:
ObjectSpace.each_object(Thing) {|x| print x}

You could also use weak references, but Object Space is
much easier most of the time.

-- 
Tomasz Wegrzanowski [ http://t-a-w.blogspot.com/ ]