[DOC] Hash defaults doc

This commit is contained in:
Burdette Lamar 2025-01-22 14:12:53 -06:00 committed by GitHub
parent c290861336
commit 6f6654e504
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
Notes: git 2025-01-22 20:13:12 +00:00
Merged: https://github.com/ruby/ruby/pull/12560

Merged-By: peterzhu2118 <peter@peterzhu.ca>

137
hash.c
View File

@ -2098,7 +2098,7 @@ rb_hash_stlike_lookup(VALUE hash, st_data_t key, st_data_t *pval)
* h[:foo] # => 0 * h[:foo] # => 0
* *
* If +key+ is not found, returns a default value * If +key+ is not found, returns a default value
* (see {Default Values}[rdoc-ref:Hash@Default+Values]): * (see {Hash Default}[rdoc-ref:Hash@Hash+Default]):
* h = {foo: 0, bar: 1, baz: 2} * h = {foo: 0, bar: 1, baz: 2}
* h[:nosuch] # => nil * h[:nosuch] # => nil
*/ */
@ -2207,7 +2207,7 @@ rb_hash_fetch(VALUE hash, VALUE key)
* *
* Returns the default value for the given +key+. * Returns the default value for the given +key+.
* The returned value will be determined either by the default proc or by the default value. * The returned value will be determined either by the default proc or by the default value.
* See {Default Values}[rdoc-ref:Hash@Default+Values]. * See {Hash Default}[rdoc-ref:Hash@Hash+Default].
* *
* With no argument, returns the current default value: * With no argument, returns the current default value:
* h = {} * h = {}
@ -2244,7 +2244,7 @@ rb_hash_default(int argc, VALUE *argv, VALUE hash)
* h.default = false # => false * h.default = false # => false
* h.default # => false * h.default # => false
* *
* See {Default Values}[rdoc-ref:Hash@Default+Values]. * See {Hash Default}[rdoc-ref:Hash@Hash+Default].
*/ */
static VALUE static VALUE
@ -2260,7 +2260,7 @@ rb_hash_set_default(VALUE hash, VALUE ifnone)
* hash.default_proc -> proc or nil * hash.default_proc -> proc or nil
* *
* Returns the default proc for +self+ * Returns the default proc for +self+
* (see {Default Values}[rdoc-ref:Hash@Default+Values]): * (see {Hash Default}[rdoc-ref:Hash@Hash+Default]):
* h = {} * h = {}
* h.default_proc # => nil * h.default_proc # => nil
* h.default_proc = proc {|hash, key| "Default value for #{key}" } * h.default_proc = proc {|hash, key| "Default value for #{key}" }
@ -2281,7 +2281,7 @@ rb_hash_default_proc(VALUE hash)
* hash.default_proc = proc -> proc * hash.default_proc = proc -> proc
* *
* Sets the default proc for +self+ to +proc+ * Sets the default proc for +self+ to +proc+
* (see {Default Values}[rdoc-ref:Hash@Default+Values]): * (see {Hash Default}[rdoc-ref:Hash@Hash+Default]):
* h = {} * h = {}
* h.default_proc # => nil * h.default_proc # => nil
* h.default_proc = proc { |hash, key| "Default value for #{key}" } * h.default_proc = proc { |hash, key| "Default value for #{key}" }
@ -2690,8 +2690,8 @@ rb_hash_except(int argc, VALUE *argv, VALUE hash)
* h = {foo: 0, bar: 1, baz: 2} * h = {foo: 0, bar: 1, baz: 2}
* h.values_at(:baz, :foo) # => [2, 0] * h.values_at(:baz, :foo) # => [2, 0]
* *
* The {default values}[rdoc-ref:Hash@Default+Values] are returned * The {hash default}[rdoc-ref:Hash@Hash+Default] is returned
* for any keys that are not found: * for each key that is not found:
* h.values_at(:hello, :foo) # => [nil, 0] * h.values_at(:hello, :foo) # => [nil, 0]
*/ */
@ -4593,7 +4593,7 @@ rb_hash_any_p(int argc, VALUE *argv, VALUE hash)
* h = {foo: {bar: [:a, :b, :c]}} * h = {foo: {bar: [:a, :b, :c]}}
* h.dig(:foo, :bar, 2) # => :c * h.dig(:foo, :bar, 2) # => :c
* *
* This method will use the {default values}[rdoc-ref:Hash@Default+Values] * This method will use the {hash default}[rdoc-ref:Hash@Hash+Default]
* for keys that are not present: * for keys that are not present:
* h = {foo: {bar: [:a, :b, :c]}} * h = {foo: {bar: [:a, :b, :c]}}
* h.dig(:hello) # => nil * h.dig(:hello) # => nil
@ -6881,93 +6881,88 @@ static const rb_data_type_t env_data_type = {
* *
* reviews.length #=> 1 * reviews.length #=> 1
* *
* === Default Values * === Key Not Found?
* *
* The methods #[], #values_at and #dig need to return the value associated to a certain key. * When a method tries to retrieve and return the value for a key and that key <i>is found</i>,
* When that key is not found, that value will be determined by its default proc (if any) * the returned value is the value associated with the key.
* or else its default (initially `nil`).
* *
* You can retrieve the default value with method #default: * But what if the key <i>is not found</i>?
* In that case, certain methods will return a default value while other will raise a \KeyError.
* *
* h = Hash.new * ==== Nil Return Value
* h.default # => nil
* *
* You can set the default value by passing an argument to method Hash.new or * If you want +nil+ returned for a not-found key, you can call:
* with method #default=
* *
* h = Hash.new(-1) * - #[](key) (usually written as <tt>#[key]</tt>.
* h.default # => -1 * - #assoc(key).
* h.default = 0 * - #dig(key, *identifiers).
* h.default # => 0 * - #values_at(*keys).
* *
* This default value is returned for #[], #values_at and #dig when a key is * You can override these behaviors for #[], #dig, and #values_at (but not #assoc);
* not found: * see {Hash Default}[rdoc-ref:Hash@Hash+Default].
* *
* counts = {foo: 42} * ==== \KeyError
* counts.default # => nil (default)
* counts[:foo] = 42
* counts[:bar] # => nil
* counts.default = 0
* counts[:bar] # => 0
* counts.values_at(:foo, :bar, :baz) # => [42, 0, 0]
* counts.dig(:bar) # => 0
* *
* Note that the default value is used without being duplicated. It is not advised to set * If you want KeyError raised for a not-found key, you can call:
* the default value to a mutable object:
* *
* synonyms = Hash.new([]) * - #fetch(key).
* synonyms[:hello] # => [] * - #fetch_values(*keys).
* synonyms[:hello] << :hi # => [:hi], but this mutates the default!
* synonyms.default # => [:hi]
* synonyms[:world] << :universe
* synonyms[:world] # => [:hi, :universe], oops
* synonyms.keys # => [], oops
* *
* To use a mutable object as default, it is recommended to use a default proc * ==== \Hash Default
* *
* ==== Default Proc * For certain methods (#[], #dig, and #values_at),
* the return value for a not-found key is determined by two hash properties:
* *
* When the default proc for a +Hash+ is set (i.e., not +nil+), * - <i>default value</i>: returned by method #default.
* the default value returned by method #[] is determined by the default proc alone. * - <i>default proc</i>: returned by method #default_proc.
* *
* You can retrieve the default proc with method #default_proc: * In the simple case, both values are +nil+,
* and the methods return +nil+ for a not-found key;
* see {Nil Return Value}[rdoc-ref:Hash@Nil+Return+Value] above.
* *
* h = Hash.new * Note that this entire section ("Hash Default"):
* h.default_proc # => nil
* *
* You can set the default proc by calling Hash.new with a block or * - Applies _only_ to methods #[], #dig, and #values_at.
* calling the method #default_proc= * - Does _not_ apply to methods #assoc, #fetch, or #fetch_values,
* which are not affected by the default value or default proc.
* *
* h = Hash.new { |hash, key| "Default value for #{key}" } * ===== Any-Key Default
* h.default_proc.class # => Proc
* h.default_proc = proc { |hash, key| "Default value for #{key.inspect}" }
* h.default_proc.class # => Proc
* *
* When the default proc is set (i.e., not +nil+) * You can define an any-key default for a hash;
* and method #[] is called with with a non-existent key, * that is, a value that will be returned for _any_ not-found key:
* #[] calls the default proc with both the +Hash+ object itself and the missing key,
* then returns the proc's return value:
* *
* h = Hash.new { |hash, key| "Default value for #{key}" } * - The value of #default_proc <i>must be</i> +nil+.
* h[:nosuch] # => "Default value for nosuch" * - The value of #default (which may be any object, including +nil+)
* will be returned for a not-found key.
* *
* Note that in the example above no entry for key +:nosuch+ is created: * You can set the default value when the hash is created with Hash.new and option +default_value+,
* or later with method #default=.
* *
* h.include?(:nosuch) # => false * Note: although the value of #default may be any object,
* it may not be a good idea to use a mutable object.
* *
* However, the proc itself can add a new entry: * ===== Per-Key Defaults
* *
* synonyms = Hash.new { |hash, key| hash[key] = [] } * You can define a per-key default for a hash;
* synonyms.include?(:hello) # => false * that is, a Proc that will return a value based on the key itself.
* synonyms[:hello] << :hi # => [:hi]
* synonyms[:world] << :universe # => [:universe]
* synonyms.keys # => [:hello, :world]
* *
* Note that setting the default proc will clear the default value and vice versa. * You can set the default proc when the hash is created with Hash.new and a block,
* or later with method #default_proc=.
* *
* Be aware that a default proc that modifies the hash is not thread-safe in the * Note that the proc can modify +self+,
* sense that multiple threads can call into the default proc concurrently for the * but modifying +self+ in this way is not thread-safe;
* same key. * multiple threads can concurrently call into the default proc
* for the same key.
*
* ==== \Method Default
*
* For two methods, you can specify a default value for a not-found key
* that has effect only for a single method call
* (and not for any subsequent calls):
*
* - For method #fetch, you can specify an any-key default:
* - For either method #fetch or method #fetch_values,
* you can specify a per-key default via a block.
* *
* === What's Here * === What's Here
* *