On Sat, Jan 21, 2012 at 12:30 AM, Frank Guerino <frank.guerino / if4it.com> w=
rote:
> This seems to work!
>
>
> class Color
> =A0def initialize (address, color, colorHash)
> =A0 =A0@localColor =3D color
> =A0 =A0@localAddress =3D address
> =A0 =A0#colorHash[@localAddress].push(self)
> =A0 =A0colorHash.store(@localAddress, self)
> =A0end
>
> =A0def getColor
> =A0 =A0@localColor
> =A0end
> end
>
> hashObject =3D {}
>
> 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 =3D {}

  # class method
  def self.at_address(address)
    @@color_hash[address].color
  end

  def initialize (address, color)
    @color =3D color
    @address =3D address
    @@color_hash[@address] =3D 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")

#=3D=3D=3D=3D

Abinoam Jr.