From: Josh Cheek Date: 2012-01-21T19:47:54+09:00 Subject: Re: Getting an Object to Push or Register "Itself" With a Hash During Initialization --f46d04451999672b3604b70787d4 Content-Type: text/plain; charset=ISO-8859-1 On Fri, Jan 20, 2012 at 4:43 PM, Frank Guerino wrote: > Hi, > > How do I tell an object to register "itself" with a hash that has been > passed in during initialization? > > For example, I have: > > # START --------------------------- > hashOfHomes = {} > > Class House > def initialize homeAddress, homeHash > @localHomeAddressString = homeAddress > HOW DO I PUSH "THIS" OBJECT (I.E> "SELF") ONTO THE HASH DURING THIS > INITIALIZATION???? > end > end > > home = House.new( "123 Main Street", hashOfHomes) > # END --------------------------- > > I've read through multiple references on object creation and "self" but > don't see anything that is a clear answer. > > Thanks, in advance, for any assistance you can offer! > > Frank > > -- > Posted via http://www.ruby-forum.com/. > > Not really sure what push is supposed to do (I don't know C++, tried to look it up but don't see anything about push http://www.cplusplus.com/reference/stl/map/) But generally, you set it like this: hash[key] = value For your case, that would probably look something like this: class House # class methods @registrations = {} def self.register(address, house) @registrations[address] = house end def self.at(address) @registrations[address] end # instance methods def initialize(address) @address = address self.class.register address, self end def inspect "#" end end home = House.new "123 Main Street" House.at "123 Main Street" # => # --f46d04451999672b3604b70787d4--