From: Peter Hickman Date: 2013-05-06T16:19:12+09:00 Subject: Re: Hash return is not correct --001a11c1b8d456a8b804dc0784ff Content-Type: text/plain; charset=ISO-8859-1 No the output is correct. Your mistake is assuming that Router.new will return the same object for the same input parameters. For example: frequency = Hash.new(0) models = %w(2000 3000 2000 3200) models.each{|m| frequency[Router.new('Linksys',m)] += 1} p frequency => { #=>1, #=>1, #=>1, #=>1 } Note each entry is a different object. This is the normal and correct behaviour. When you do: puts frequency[Router.new('Linksys','2000')] I assume you meant this and not Car.new it creates yet another object which of course is not in the hash and so returns 0. --001a11c1b8d456a8b804dc0784ff Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
No the output is correct. Your mistake is assuming that Ro= uter.new will return the same object for the same input parameters. For exa= mple:

frequency =3D Hash.new(0)
models =3D %w(2= 000 3000 2000 3200)
models= .each{|m| frequency[Router.new('Linksys',m)] +=3D 1}

=

p=A0frequency =3D>=A0{

#<Router:0x10f80ae70 @model=3D"3000", @make=3D&q= uot;Linksys">=3D>1,=A0

#<Router:0x10f80ae20 = @model=3D"2000", @make=3D"Linksys">=3D>1,=A0

<= p class=3D"">#<Router:0x10f80aec0 @model=3D"2000", @make=3D&qu= ot;Linksys">=3D>1,=A0

#<Router:0x10f80add0 @model=3D"3200", @make=3D&q= uot;Linksys">=3D>1

}


<= p class=3D"" style>Note each entry is a different object. This is the norma= l and correct=A0behaviour. When you do:

puts frequency[Router.new('Linksys','2000')]

I assume you meant this and not Car.new it creates yet another obj= ect which of course is not in the hash and so returns 0.


=
--001a11c1b8d456a8b804dc0784ff--