Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Hash.new(initial_capacity, &block) doc to use relevant example #14429

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading