From: dblack@... Date: 2007-07-04T20:10:22+09:00 Subject: Re: Isn't hash default value behavior just a little bizarre? ---2049402039-369337158-1183547419=:6742 Content-Type: MULTIPART/MIXED; BOUNDARY="-2049402039-369337158-1183547419=:6742" This message is in MIME format. The first part should be readable text, while the remaining parts are likely unreadable without MIME-aware tools. ---2049402039-369337158-1183547419=:6742 Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE Hi -- On Wed, 4 Jul 2007, Farhad Farzaneh wrote: > Pe=C3=B1a, Botp wrote: > >> >> ruby allows one to change the default value. that is good, imho. >> i think the surprise stems fr the fact that it is _too simple to change >> the default. hey, it's ruby ;) >> >> kind regards -botp > > You're right. Somehow I missed that in the documentation.... did RTFM, > but obviously not carefully enough. > > Still, I do find it pretty surprising. All you're doing is assigning an object to a certain role. There's no implication of automatic duplication of the object. > I got stung when I was actually > using the feature to initialize any entry to an array just to avoid the > business of > > hash[key] ||=3D [] It doesn't do that anyway: h =3D {} h.default =3D [] p h["blah"] # [] (default value for undefined key) p h # {} (no keys are defined) The default value is for undefined keys, whereas hash[key] ||=3D [] actually sets a key. 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) ---2049402039-369337158-1183547419=:6742-- ---2049402039-369337158-1183547419=:6742--