From: Christopher Dicely Date: 2009-03-08T18:47:11+09:00 Subject: Re: Can I access or find a object from it's instance variable? On Sun, Mar 8, 2009 at 1:29 AM, Leo wrote: >> 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. Yeah, in most real-world cases using this basic design, you're probably going to want to either use weak references (and add code to manage them so that dead references are silently cleaned up rather than throwing exceptions, or provide an explicit ability to delete an instance from the class-level index, depending on how you expect objects of the class to be used.