From 37361d87a6903c16d3e0662ae6a7e285f3ede68b Mon Sep 17 00:00:00 2001 From: Burdette Lamar Date: Sun, 5 Jan 2025 08:40:49 -0600 Subject: [PATCH] [DOC] Tweaks for Hash.new (#12503) --- hash.rb | 50 +++++++++++++++++++++++--------------------------- 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/hash.rb b/hash.rb index 08edb745a4..cc42252f12 100644 --- a/hash.rb +++ b/hash.rb @@ -1,39 +1,35 @@ class Hash # call-seq: - # Hash.new(default_value = nil) -> new_hash - # Hash.new(default_value = nil, capacity: size) -> new_hash - # Hash.new {|hash, key| ... } -> new_hash - # Hash.new(capacity: size) {|hash, key| ... } -> new_hash + # Hash.new(default_value = nil, capacity: 0) -> 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, + # which determine the value to be returned by method Hash#[] when the entry does not exist. # - # The initial default value and initial default proc for the new hash - # depend on which form above was used. See {Default Values}[rdoc-ref:Hash@Default+Values]. + # By default, a hash has +nil+ values for both +default+ and +default_proc+: # - # If neither an argument nor a block is given, - # initializes both the default value and the default proc to nil: - # h = Hash.new - # h.default # => nil - # h.default_proc # => nil + # h = Hash.new # => {} + # h.default # => nil + # h.default_proc # => nil # - # If argument default_value is given but no block is given, - # initializes the default value to the given default_value - # and the default proc to nil: - # h = Hash.new(false) - # h.default # => false - # h.default_proc # => nil + # If the +default_value+ argument is provided, it sets the +default+ value for the hash: # - # If a block is given but no default_value, stores the block as the default proc - # and sets the default value to nil: - # h = Hash.new {|hash, key| "Default value for #{key}" } - # h.default # => nil - # h.default_proc.class # => Proc - # h[:nosuch] # => "Default value for nosuch" + # h = Hash.new(false) # => {} + # h.default # => false + # h.default_proc # => nil # - # If both a block and a default_value are given, raises an +ArgumentError+ + # If a block is given, it sets the +default_proc+ value: # - # If the optional keyword argument +capacity+ is given, the hash will be allocated - # with enough capacity to accommodate this many keys without having to be resized. + # h = Hash.new {|hash, key| "Hash #{hash}: Default value for #{key}" } + # h.default # => nil + # h.default_proc # => # + # h[:nosuch] # => "Hash {}: Default value for nosuch" + # + # 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. + # + # See also {Methods for Creating a Hash}[rdoc-ref:Hash@Methods+for+Creating+a+Hash]. def initialize(ifnone = (ifnone_unset = true), capacity: 0, &block) Primitive.rb_hash_init(capacity, ifnone_unset, ifnone, block) end