Keep track of all instances of a class without losing GC?

Hi,

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?

Thanks for your help

···

--
Posted via http://www.ruby-forum.com/.

Have a look at weakref.rb. Either use that directly, or copy ideas from
weakref.rb to create your own system.

--Ken Bloom

···

On Sun, 27 Aug 2006 23:45:34 +0900, Jez Stephens wrote:

Hi,

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?

Thanks for your help

--
Ken Bloom. PhD candidate. Linguistic Cognition Laboratory.
Department of Computer Science. Illinois Institute of Technology.
http://www.iit.edu/~kbloom1/

require 'weakref'

check out the docs.

-a

···

On Sun, 27 Aug 2006, Jez Stephens wrote:

Hi,

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?

Thanks for your help

--
to foster inner awareness, introspection, and reasoning is more efficient than
meditation and prayer.
- h.h. the 14th dalai lama

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.

···

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?

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

weakref might be of help indeed, you also can use lookups (3 extra lines):

class Thing
    @@things = []
     def self.things
         @@things.map{ | id | ObjectSpace._id2ref(id) }
     end

     def initialize
         @@things << self.object_id
     end
end

After that you might get lost references when object get garbage collected, but if you don't need that - what's the point ? :slight_smile:

···

On 27-aug-2006, at 16:45, Jez Stephens 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

--
Julian 'Julik' Tarkhanov
please send all personal mail to
me at julik.nl