Getting an Object to Push or Register "Itself" With a Hash During Initialization

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

On Fri, Jan 20, 2012 at 2:43 PM, Frank G.
[email protected]wrote:

Class House
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/.

Something like this will work:

hash = {}
=> {}
class A
def initialize(n, hash)
hash.merge! n => self
end
end
=> nil
A.new(1, hash)
=> #<A:0x007f90b3159d20>
hash
=> {1=>#<A:0x007f90b3159d20>}

pete

BTW, I wanted to clarify and make it clear that I want to use the
Address that is passed into the House class as the key in the Hash
structure.

I would think that the following command would work.

 homeHash[@localHomeAddressString].push(self)

However, “self” does not seem to be doing what I think it should.

In C++, using the “this” construct seems so much cleaner.

Thanks again,

Frank

Hi Pete,

Thanks for the follow up. For clarification, what exactly is happening
when you use the “hash.merge!” command?

Thanks for your help,

Frank

This seems to work!

class Color
def initialize (address, color, colorHash)
@localColor = color
@localAddress = address
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

Thanks!

Frank

On Sat, Jan 21, 2012 at 12:30 AM, Frank G.
[email protected] wrote:

Posted via http://www.ruby-forum.com/.

Hi Frank,

Try…

colorHash[@localAddress] = self

About the…

homeHash[@localHomeAddressString].push(self)

  1. Hash doesn’t have any ‘push’ method (this is for Array).
  2. homeHash[@localHomeAddressString] is probably returning ‘nil’
    because this key doesn’t have any value associated with it. After
    that, ruby tries to send ‘push’ method to this returned object.

Abinoam Jr.

On Sat, Jan 21, 2012 at 12:30 AM, Frank G.
[email protected] wrote:

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.

This is how I do it and it works fine. Notice that I do not do the
registration while initialising the object but with another method. This
is because (as far as I understand) while the initialise method is
executed by the object generator of the class, the object was not
actually created yet.

def onstream(stream_object,pond_label)
raise “#{pond_label} already used. Must be unique\n” unless
(stream_object.pond_registry[pond_label].nil?)

   stream_object.pond_registry[pond_label] = self

end

Hello All,

Thanks for all your help. I’ve learned about multiple options from each
of you that have me thinking about multiple solution permutations that
will work for what I’m trying to do.

Ultimately, I need each new object of a certain class type to register
itself with a centralized Hash construct, using a unique address key, so
that I can access any one of the objects, later, by the address key.

With your help, I can do that, now. I appreciate your efforts!

My Best,

Frank

Oren S. wrote in post #1041899:

This is how I do it and it works fine. Notice that I do not do the
registration while initialising the object but with another method. This
is because (as far as I understand) while the initialise method is
executed by the object generator of the class, the object was not
actually created yet.

No: the object is already created (allocated). The initialize method
is there for you to set it up as desired, but ‘self’ is a completely
formed object and it can call its own methods during initialize, and/or
pass itself to other objects.

Whether this is a good design practice is another issue; it may make
your objects less reusable if they are forcibly linked to a single
container object.

Another option is to use a method of the container to create and add the
objects: this lets you create free objects, and/or objects in distinct
containers.

class Container
def initialize
@collection = {}
end

def create_foo(id, *args)
@collection[id] = Foo.new(*args)
end
end

On Fri, Jan 20, 2012 at 4:43 PM, Frank G.
[email protected]wrote:

Class House
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>

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs