On Fri, Jan 20, 2012 at 4:43 PM, Frank Guerino <frank.guerino / if4it.com>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
    "#<House at #{@address}>"
  end
end

home = House.new "123 Main Street"
House.at "123 Main Street" # => #<House at 123 Main Street>