From: dblack@... Date: 2007-09-05T19:49:57+09:00 Subject: Re: assigning to hash keys when there is a default value? --1926193751-478355187-1188989391=:1699 Content-Type: MULTIPART/MIXED; BOUNDARY="1926193751-478355187-1188989391=:1699" 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-478355187-1188989391=:1699 Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed Content-Transfer-Encoding: QUOTED-PRINTABLE Hi -- On Wed, 5 Sep 2007, Robert Klemme wrote: > 2007/9/5, Pe=F1a, Botp : >> From: Robert Klemme [mailto:shortcutter@googlemail.com] >> # 2007/9/4, Yossef Mendelssohn : >> # > If '"x+=3D1" will always be "x=3Dx+1"', what's the problem in >> # having x ||=3D >> # > 1 always be x =3D x || 1? >> # That an assignment of x=3Dx is useless (basically a nop in the standar= d >> # case of x being a variable). >> >> consider, >> >> irb(main):074:0* h=3DHash.new >> =3D> {} >> irb(main):075:0> h[5] =3D h[5] >> =3D> nil >> irb(main):076:0> h >> =3D> {5=3D>nil} >> irb(main):062:0* h=3DHash.new(1) >> =3D> {} >> irb(main):063:0> h[5] =3D h[5] >> =3D> 1 >> irb(main):064:0> h >> =3D> {5=3D>1} >> >> thus h[k] =3D h[k] could mean >> h[k] =3D h.default or >> h[k] =3D nil >> >> definitely not useless and not noop. > > I said "noop in the standard case of a variable". We can certainly > debate about usefulness or uselessness. I concede that it's not free > of side effects in the case of a Hash, but the only noticeable side > effect is the change of the key set. Assigning with the Hash's > default is not visible through Hash#[] although it is with #fetch > which seems to be rarely used from what I see. So I still say that > usefulness of self assignment is very limited. I'd be happy for unnecessary assignments to be optimized away, but I like the conceptual clarity of the x =3D x || y interpretation for x ||=3D y. 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-478355187-1188989391=:1699-- --1926193751-478355187-1188989391=:1699--