Feature #10270
closedHash#insert
Description
Hash doesn't appear to have a "safe" way to insert an entry that won't clobber another key if it is already there. So how about:
Updated by matz (Yukihiro Matsumoto) almost 12 years ago
- Status changed from Open to Feedback
-
I am not sure #insert is the best name for non clobbering merge.
-
do you know merge takes a block to resolve key conflict?
h.merge(h2) {|key,v1,v2|v1}
works as your proposed #insert.
Matz.
Updated by atlas (Atlas Prime) almost 12 years ago
The name "insert" seems good to me because it implies a new entry and unable to change an old entry. But key-value pair must be thought of as an entry too for it to make sense (I guess I get that notion from Java http://docs.oracle.com/javase/7/docs/api/java/util/Map.Entry.html).
I did not know that merge took this block. That could be useful. But it's not quite the same because insert returns true/false if successful of not. It's utility is mostly as a short cut for
Instead one can do
However, you make me think it might be useful too if it could take a hash and a block.