From: dblack@... Date: 2007-09-03T20:37:13+09:00 Subject: Re: assigning to hash keys when there is a default value? --1926193751-1236775918-1188819428=:20075 Content-Type: MULTIPART/MIXED; BOUNDARY="1926193751-1236775918-1188819428=:20075" This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. --1926193751-1236775918-1188819428=:20075 Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE Hi -- On Mon, 3 Sep 2007, 7stud -- wrote: > Pe=C3=B1a, Botp wrote: >> they should be different (note, that has nothing to do w the bug dBlack >> is pointing at) and the output should be, >> >> 5 >> {2=3D>5} #<---dblack is complaining here because the ruby output is {= } >> >> nil >> {2=3D>10} >> > > Yes. That is what I am complaining about too! Your are right: the > second line for each section of the output should be different--I > mispoke. What I want to know is why the the output is {} and not > {2=3D>5}. > > The author of "The Ruby Way (2nd ed)" presents the two examples as well > as the different output for each example, yet he doesn't explain the > reason for the different output. He suggests that you can use ||=3D to > selectively assign values only to keys that don't exist. The point I > think he was trying to make with the two examples is that when you have > a default value for non-existent keys, all keys "exist", so ||=3D will no= t > create new keys with the default value. I don't have the book at hand but the default value definitely doesn't mean that all keys exist (or even "exist" :-) irb(main):003:0> h =3D Hash.new(5) =3D> {} irb(main):004:0> h[1] =3D> 5 irb(main):005:0> h.has_key?(1) =3D> false The only possible explanation I can come up with, which I don't like, is that: h[2] ||=3D 10 is being treated like: 5 =3D 5 || 10 which is just another can of worms. David --=20 * Books: RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242) RUBY FOR RAILS (http://www.manning.com/black) * Ruby/Rails training & consulting: Ruby Power and Light, LLC (http://www.rubypal.com) --1926193751-1236775918-1188819428=:20075-- --1926193751-1236775918-1188819428=:20075--