[DOC] Tweaks for Hash.new

This commit is contained in:
BurdetteLamar 2025-01-23 08:56:15 -06:00 committed by Peter Zhu
parent f6e259da87
commit 22e5d83cd1
Notes: git 2025-01-24 15:44:41 +00:00

10
hash.rb
View File

@ -4,8 +4,10 @@ class Hash
# Hash.new(capacity: 0) {|self, key| ... } -> new_hash # Hash.new(capacity: 0) {|self, key| ... } -> new_hash
# #
# Returns a new empty \Hash object. # Returns a new empty \Hash object.
#
# Initializes the values of Hash#default and Hash#default_proc, # Initializes the values of Hash#default and Hash#default_proc,
# which determine the value to be returned by method Hash#[] when the entry does not exist. # which determine the behavior when a given key is not found;
# see {Key Not Found?}[rdoc-ref:Hash@Key+Not+Found-3F].
# #
# By default, a hash has +nil+ values for both +default+ and +default_proc+: # By default, a hash has +nil+ values for both +default+ and +default_proc+:
# #
@ -13,19 +15,21 @@ class Hash
# h.default # => nil # h.default # => nil
# h.default_proc # => nil # h.default_proc # => nil
# #
# If the +default_value+ argument is provided, it sets the +default+ value for the hash: # With argument +default_value+ given, sets the +default+ value for the hash:
# #
# h = Hash.new(false) # => {} # h = Hash.new(false) # => {}
# h.default # => false # h.default # => false
# h.default_proc # => nil # h.default_proc # => nil
# #
# If a block is given, it sets the +default_proc+ value: # With a block given, sets the +default_proc+ value:
# #
# h = Hash.new {|hash, key| "Hash #{hash}: Default value for #{key}" } # h = Hash.new {|hash, key| "Hash #{hash}: Default value for #{key}" }
# h.default # => nil # h.default # => nil
# h.default_proc # => #<Proc:0x00000289b6fa7048 (irb):185> # h.default_proc # => #<Proc:0x00000289b6fa7048 (irb):185>
# h[:nosuch] # => "Hash {}: Default value for nosuch" # h[:nosuch] # => "Hash {}: Default value for nosuch"
# #
# Raises ArgumentError if both +default_value+ and a block are given.
#
# If optional keyword argument +capacity+ is given with a positive integer value +n+, # If optional keyword argument +capacity+ is given with a positive integer value +n+,
# initializes the hash with enough capacity to accommodate +n+ entries without resizing. # initializes the hash with enough capacity to accommodate +n+ entries without resizing.
# #