From: Robert Klemme Date: 2007-09-05T18:49:14+09:00 Subject: Re: assigning to hash keys when there is a default value? 2007/9/5, Pe�a, Botp : > From: Robert Klemme [mailto:shortcutter@googlemail.com] > # 2007/9/4, Yossef Mendelssohn : > # > If '"x+=1" will always be "x=x+1"', what's the problem in > # having x ||= > # > 1 always be x = x || 1? > # That an assignment of x=x is useless (basically a nop in the standard > # case of x being a variable). > > consider, > > irb(main):074:0* h=Hash.new > => {} > irb(main):075:0> h[5] = h[5] > => nil > irb(main):076:0> h > => {5=>nil} > irb(main):062:0* h=Hash.new(1) > => {} > irb(main):063:0> h[5] = h[5] > => 1 > irb(main):064:0> h > => {5=>1} > > thus h[k] = h[k] could mean > h[k] = h.default or > h[k] = 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. Kind regards robert