From: Leo Date: 2009-03-08T18:29:11+09:00 Subject: Re: Can I access or find a object from it's instance variable? > class Foo >   attr_reader :key > >   @@instances = Hash.new {|h,k| h[k] = []} > [...] >   def initialize(key_val) >     @key = key_val >     @@instances[key_val] << self >   end > end I don't know ruby well enough but I'd assume a weak reference would be preferable -- unless the script won't run very long anyway. In general, if you want control over the instances of a class, you could also consider something singleton like: require 'weakref' class Foo attr_reader :key @instances = {} def initialize(key) @key = key end def self.instance(key) find(key) || Foo.new(key).tap {|o| @instances[key] = WeakRef.new(o)} end def self.find(key) if @instances.has_key?(key) and @instances[key].weakref_alive? @instances[key].__getobj__ end end end a = Foo.instance("a") a1 = Foo.instance("a") a2 = Foo.find("a") p a.object_id, a1.object_id, a2.object_id