@@ -266,6 +266,16 @@ class Hash(K, V)
266266 # Creates a new empty `Hash` with a *block* that handles missing keys.
267267 #
268268 # ```
269+ # inventory = Hash(String, Int32).new(0)
270+ # inventory["socks"] = 3
271+ # inventory["pickles"] # => 0
272+ # ```
273+ #
274+ # WARNING: When the default block is invoked on a missing key, its return
275+ # value is *not* implicitly stored into the hash under that key. If you want
276+ # that behaviour, you need to put it explicitly:
277+ #
278+ # ```
269279 # hash = Hash(String, Int32).new do |hash, key|
270280 # hash[key] = key.size
271281 # end
@@ -294,14 +304,23 @@ class Hash(K, V)
294304 # inventory["pickles"] # => 0
295305 # ```
296306 #
297- # NOTE: The default value is passed by reference:
307+ # WARNING: When the default value gets returned on a missing key, it is *not*
308+ # stored into the hash under that key. If you want that behaviour, please use
309+ # the overload with a block.
310+ #
311+ # WARNING: The default value is returned as-is. It gets neither duplicated nor
312+ # cloned. For types with reference semantics this means it will be exactly the
313+ # *same* object every time.
314+ #
298315 # ```
299- # arr = [1, 2, 3]
300- # hash = Hash(String, Array(Int32)).new(arr)
301- # hash["3"][1] = 4
302- # arr # => [1, 4, 3]
316+ # hash = Hash(String, Array(Int32)).new([1])
317+ # hash["a"][0] = 2
318+ # hash["b"] # => [2]
303319 # ```
304320 #
321+ # * `.new(&block : (Hash(K, V), K -> V))` is an alternative with a block that
322+ # can return a different default value for each invocation.
323+ #
305324 # The *initial_capacity* is useful to avoid unnecessary reallocations
306325 # of the internal buffer in case of growth. If the number of elements
307326 # a hash will hold is known, the hash should be initialized with that
0 commit comments