From: "Abinoam Jr." Date: 2012-01-21T13:19:13+09:00 Subject: Re: Getting an Object to Push or Register "Itself" With a Hash During Initialization On Sat, Jan 21, 2012 at 12:30 AM, Frank Guerino wrote: > This seems to work! > > > class Color >  def initialize (address, color, colorHash) >    @localColor = color >    @localAddress = address >    #colorHash[@localAddress].push(self) >    colorHash.store(@localAddress, self) >  end > >  def getColor >    @localColor >  end > end > > hashObject = {} > > Color.new( "A", "Red", hashObject ) > Color.new( "B", "White", hashObject ) > Color.new( "C", "Blue", hashObject ) > > puts hashObject["C"].getColor > puts hashObject["B"].getColor > puts hashObject["A"].getColor Can you encapsulate this hashObject Hash "inside" the class itself? Look at this snippet bellow. (I tried to make the code almost the same of yours (line by line)) class Color #class variable @@color_hash = {} # class method def self.at_address(address) @@color_hash[address].color end def initialize (address, color) @color = color @address = address @@color_hash[@address] = self end def color @color end end # You don't need to send the (same) hashObject at initialization. It was a repetition. Color.new( "A", "Red" ) Color.new( "B", "White" ) Color.new( "C", "Blue" ) # You dont' need to call the getColor. Because at_address already does this for you. # and, calling a method on the Color class itself makes it more readable than hashObject (IMHO). puts Color.at_address("C") puts Color.at_address("B") puts Color.at_address("A") #==== Abinoam Jr.