From: "MartinBosslet (Martin Bosslet)" Date: 2012-03-29T08:09:54+09:00 Subject: [ruby-core:43825] [ruby-trunk - Feature #6219] Return value of Hash#store Issue #6219 has been updated by MartinBosslet (Martin Bosslet). cjheath (Clifford Heath) wrote: > On 29/03/2012, at 8:40 AM, MartinBosslet (Martin Bosslet) wrote: > > Right, good point. OK, then let me rephrase it for explicitly calling store. > ... > > That way I can check for collisions in one pass without having to call has_key? first. > > It's a nice thought, but about fifteen years too late - you can't just change > fundamental things like this without introducing all sorts of subtle bugs into > people's programs. Write a new method (e.g. put(x)) that has the behaviour > you want. > Doing so in one pass is only possible on the C level. No use in composing it in Ruby, this still means that the hash function is evaluated twice. Hash is probably way too popular for no one to rely on the current return value, I agree. But put isn't taken yet - how about Hash#put with the described behavior? ---------------------------------------- Feature #6219: Return value of Hash#store https://bugs.ruby-lang.org/issues/6219#change-25330 Author: MartinBosslet (Martin Bosslet) Status: Open Priority: Low Assignee: Category: core Target version: Hash#store returns the value that was just assigned, for example: h[:a] = b # => b Does anyone rely on this behavior, are there cases when this becomes handy? If however the return value is discarded most of the time, I was thinking it might be beneficial if we would return the previous value of a given key (nil if none was assigned yet) instead. That way we could assign and check for a collision in one pass, something that right now can only be done in two separate steps. -- http://bugs.ruby-lang.org/