Skip to content

Commit

Permalink
Fix Hash.new(initial_capacity, &block) doc to use relevant example (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
lachlan committed Apr 5, 2024
1 parent 8ca32e8 commit dbf7aef
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/hash.cr
Original file line number Diff line number Diff line change
Expand Up @@ -266,14 +266,21 @@ class Hash(K, V)
# Creates a new empty `Hash` with a *block* that handles missing keys.
#
# ```
# inventory = Hash(String, Int32).new(0)
# inventory["socks"] = 3
# inventory["pickles"] # => 0
# hash = Hash(String, String).new do |hash, key|
# "some default value"
# end
#
# hash.size # => 0
# hash["foo"] = "bar" # => "bar"
# hash.size # => 1
# hash["baz"] # => "some default value"
# hash.size # => 1
# hash # => {"foo" => "bar"}
# ```
#
# WARNING: When the default block is invoked on a missing key, its return
# value is *not* implicitly stored into the hash under that key. If you want
# that behaviour, you need to put it explicitly:
# that behaviour, you need to store it explicitly:
#
# ```
# hash = Hash(String, Int32).new do |hash, key|
Expand Down

0 comments on commit dbf7aef

Please sign in to comment.