From: Daniel Baird Date: 2006-03-15T16:17:52+09:00 Subject: Re: Hash default -- what's going on here? ------=_Part_6549_12041498.1142407055374 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline I'm guessing it's got something to do with your assignment table[a]=3Dt the array that is the hash's default can be assigned to. I think that's what you're doing. If i did some testing I might be more sure :) ;Daniel On 15/03/06, Pete Goodeve wrote: > > > I've hit what is either an egregious bug in ruby (1.8.4) or some subtlety > that I haven't grasped. [Most likely the latter... (:-)] > > Briefly I had two arrays of symbol strings of which I wanted to find > which symbols occurred in both arrays, so I thought I'd use a hash with > the key being the symbol and the value a two element array that would > be used to flag which set(s) the symbol was found in. The following > is a simplified (and 'instrumented') version of what I was trying to > do: > > listA=3D["line1","line2","line3"] > listB=3D["line5","line3","line4"] > table=3D{} > table.default=3D[0,0] > print "table default originally: ", table.default, "\n" > > listA.each {|a| t=3Dtable[a]; print a," -- undef value: ",t; > t[0]=3D1; print " new value: ",t,"\n"; > table[a]=3Dt} > print "table default now: ", table.default, "\n" > > listB.each {|a| t=3Dtable[a]; print a," -- undef value: ",t; > t[1]=3D1; print " new value: ",t,"\n"; > table[a]=3Dt} > > print "table default now: ", table.default, "\ntable contents:\n" > table.each {|k,v| print k, ": ", v[0], v[1], "\n"} > > > Now, in the 'each' statements, the variable 'a' should (except in one > case) > not be in the hash yet, so I would expect the original provided default t= o > be returned. However, here's the printout I get: > > table default now: 00 > line1 -- undef value: 00 new value: 10 > line2 -- undef value: 10 new value: 10 > line3 -- undef value: 10 new value: 10 > table default now: 10 > line5 -- undef value: 10 new value: 11 > line3 -- undef value: 11 new value: 11 > line4 -- undef value: 11 new value: 11 > table default now: 11 > table contents: > line1: 11 > line2: 11 > line3: 11 > line4: 11 > line5: 11 > > > Dunhh? Why is the default value getting changed? I can't see that > I'm resetting it anywhere. If I do the same sort of manipulations > outside of a block (one at a time in irb for instance) things happen > as I would expect -- the default remains as set, and is returned for > any undefined key. > > Illumination appreciated. > -- Pete -- > > > -- Daniel Baird http://danielbaird.com (TiddlyW;nks! :: Whiteboard Koala :: Blog :: Things That Suck) [[My webhost uptime is ~ 92%.. if no answer pls call again later!]] ------=_Part_6549_12041498.1142407055374--