ruby/hash.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

41 lines
1.5 KiB
Ruby
Raw Normal View History

class Hash
# call-seq:
2025-01-05 08:40:49 -06:00
# Hash.new(default_value = nil, capacity: 0) -> new_hash
# Hash.new(capacity: 0) {|self, key| ... } -> new_hash
#
# Returns a new empty \Hash object.
2025-01-23 08:56:15 -06:00
#
2025-01-05 08:40:49 -06:00
# Initializes the values of Hash#default and Hash#default_proc,
2025-01-23 08:56:15 -06:00
# which determine the behavior when a given key is not found;
# see {Key Not Found?}[rdoc-ref:Hash@Key+Not+Found-3F].
2025-01-05 08:40:49 -06:00
#
# By default, a hash has +nil+ values for both +default+ and +default_proc+:
#
# h = Hash.new # => {}
# h.default # => nil
# h.default_proc # => nil
#
2025-01-23 08:56:15 -06:00
# With argument +default_value+ given, sets the +default+ value for the hash:
2025-01-05 08:40:49 -06:00
#
# h = Hash.new(false) # => {}
# h.default # => false
# h.default_proc # => nil
#
2025-01-23 08:56:15 -06:00
# With a block given, sets the +default_proc+ value:
2025-01-05 08:40:49 -06:00
#
# h = Hash.new {|hash, key| "Hash #{hash}: Default value for #{key}" }
# h.default # => nil
# h.default_proc # => #<Proc:0x00000289b6fa7048 (irb):185>
# h[:nosuch] # => "Hash {}: Default value for nosuch"
#
2025-01-23 08:56:15 -06:00
# Raises ArgumentError if both +default_value+ and a block are given.
#
2025-01-05 08:40:49 -06:00
# 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
end