Skip to content

Commit 6f6654e

Browse files
[DOC] Hash defaults doc
1 parent c290861 commit 6f6654e

File tree

1 file changed

+66
-71
lines changed

1 file changed

+66
-71
lines changed

hash.c

Lines changed: 66 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -2098,7 +2098,7 @@ rb_hash_stlike_lookup(VALUE hash, st_data_t key, st_data_t *pval)
20982098
* h[:foo] # => 0
20992099
*
21002100
* If +key+ is not found, returns a default value
2101-
* (see {Default Values}[rdoc-ref:Hash@Default+Values]):
2101+
* (see {Hash Default}[rdoc-ref:Hash@Hash+Default]):
21022102
* h = {foo: 0, bar: 1, baz: 2}
21032103
* h[:nosuch] # => nil
21042104
*/
@@ -2207,7 +2207,7 @@ rb_hash_fetch(VALUE hash, VALUE key)
22072207
*
22082208
* Returns the default value for the given +key+.
22092209
* The returned value will be determined either by the default proc or by the default value.
2210-
* See {Default Values}[rdoc-ref:Hash@Default+Values].
2210+
* See {Hash Default}[rdoc-ref:Hash@Hash+Default].
22112211
*
22122212
* With no argument, returns the current default value:
22132213
* h = {}
@@ -2244,7 +2244,7 @@ rb_hash_default(int argc, VALUE *argv, VALUE hash)
22442244
* h.default = false # => false
22452245
* h.default # => false
22462246
*
2247-
* See {Default Values}[rdoc-ref:Hash@Default+Values].
2247+
* See {Hash Default}[rdoc-ref:Hash@Hash+Default].
22482248
*/
22492249

22502250
static VALUE
@@ -2260,7 +2260,7 @@ rb_hash_set_default(VALUE hash, VALUE ifnone)
22602260
* hash.default_proc -> proc or nil
22612261
*
22622262
* Returns the default proc for +self+
2263-
* (see {Default Values}[rdoc-ref:Hash@Default+Values]):
2263+
* (see {Hash Default}[rdoc-ref:Hash@Hash+Default]):
22642264
* h = {}
22652265
* h.default_proc # => nil
22662266
* h.default_proc = proc {|hash, key| "Default value for #{key}" }
@@ -2281,7 +2281,7 @@ rb_hash_default_proc(VALUE hash)
22812281
* hash.default_proc = proc -> proc
22822282
*
22832283
* Sets the default proc for +self+ to +proc+
2284-
* (see {Default Values}[rdoc-ref:Hash@Default+Values]):
2284+
* (see {Hash Default}[rdoc-ref:Hash@Hash+Default]):
22852285
* h = {}
22862286
* h.default_proc # => nil
22872287
* h.default_proc = proc { |hash, key| "Default value for #{key}" }
@@ -2690,8 +2690,8 @@ rb_hash_except(int argc, VALUE *argv, VALUE hash)
26902690
* h = {foo: 0, bar: 1, baz: 2}
26912691
* h.values_at(:baz, :foo) # => [2, 0]
26922692
*
2693-
* The {default values}[rdoc-ref:Hash@Default+Values] are returned
2694-
* for any keys that are not found:
2693+
* The {hash default}[rdoc-ref:Hash@Hash+Default] is returned
2694+
* for each key that is not found:
26952695
* h.values_at(:hello, :foo) # => [nil, 0]
26962696
*/
26972697

@@ -4593,7 +4593,7 @@ rb_hash_any_p(int argc, VALUE *argv, VALUE hash)
45934593
* h = {foo: {bar: [:a, :b, :c]}}
45944594
* h.dig(:foo, :bar, 2) # => :c
45954595
*
4596-
* This method will use the {default values}[rdoc-ref:Hash@Default+Values]
4596+
* This method will use the {hash default}[rdoc-ref:Hash@Hash+Default]
45974597
* for keys that are not present:
45984598
* h = {foo: {bar: [:a, :b, :c]}}
45994599
* h.dig(:hello) # => nil
@@ -6881,93 +6881,88 @@ static const rb_data_type_t env_data_type = {
68816881
*
68826882
* reviews.length #=> 1
68836883
*
6884-
* === Default Values
6884+
* === Key Not Found?
68856885
*
6886-
* The methods #[], #values_at and #dig need to return the value associated to a certain key.
6887-
* When that key is not found, that value will be determined by its default proc (if any)
6888-
* or else its default (initially `nil`).
6886+
* When a method tries to retrieve and return the value for a key and that key <i>is found</i>,
6887+
* the returned value is the value associated with the key.
68896888
*
6890-
* You can retrieve the default value with method #default:
6889+
* But what if the key <i>is not found</i>?
6890+
* In that case, certain methods will return a default value while other will raise a \KeyError.
68916891
*
6892-
* h = Hash.new
6893-
* h.default # => nil
6892+
* ==== Nil Return Value
68946893
*
6895-
* You can set the default value by passing an argument to method Hash.new or
6896-
* with method #default=
6894+
* If you want +nil+ returned for a not-found key, you can call:
68976895
*
6898-
* h = Hash.new(-1)
6899-
* h.default # => -1
6900-
* h.default = 0
6901-
* h.default # => 0
6896+
* - #[](key) (usually written as <tt>#[key]</tt>.
6897+
* - #assoc(key).
6898+
* - #dig(key, *identifiers).
6899+
* - #values_at(*keys).
69026900
*
6903-
* This default value is returned for #[], #values_at and #dig when a key is
6904-
* not found:
6901+
* You can override these behaviors for #[], #dig, and #values_at (but not #assoc);
6902+
* see {Hash Default}[rdoc-ref:Hash@Hash+Default].
69056903
*
6906-
* counts = {foo: 42}
6907-
* counts.default # => nil (default)
6908-
* counts[:foo] = 42
6909-
* counts[:bar] # => nil
6910-
* counts.default = 0
6911-
* counts[:bar] # => 0
6912-
* counts.values_at(:foo, :bar, :baz) # => [42, 0, 0]
6913-
* counts.dig(:bar) # => 0
6904+
* ==== \KeyError
69146905
*
6915-
* Note that the default value is used without being duplicated. It is not advised to set
6916-
* the default value to a mutable object:
6906+
* If you want KeyError raised for a not-found key, you can call:
69176907
*
6918-
* synonyms = Hash.new([])
6919-
* synonyms[:hello] # => []
6920-
* synonyms[:hello] << :hi # => [:hi], but this mutates the default!
6921-
* synonyms.default # => [:hi]
6922-
* synonyms[:world] << :universe
6923-
* synonyms[:world] # => [:hi, :universe], oops
6924-
* synonyms.keys # => [], oops
6908+
* - #fetch(key).
6909+
* - #fetch_values(*keys).
69256910
*
6926-
* To use a mutable object as default, it is recommended to use a default proc
6911+
* ==== \Hash Default
69276912
*
6928-
* ==== Default Proc
6913+
* For certain methods (#[], #dig, and #values_at),
6914+
* the return value for a not-found key is determined by two hash properties:
69296915
*
6930-
* When the default proc for a +Hash+ is set (i.e., not +nil+),
6931-
* the default value returned by method #[] is determined by the default proc alone.
6916+
* - <i>default value</i>: returned by method #default.
6917+
* - <i>default proc</i>: returned by method #default_proc.
69326918
*
6933-
* You can retrieve the default proc with method #default_proc:
6919+
* In the simple case, both values are +nil+,
6920+
* and the methods return +nil+ for a not-found key;
6921+
* see {Nil Return Value}[rdoc-ref:Hash@Nil+Return+Value] above.
69346922
*
6935-
* h = Hash.new
6936-
* h.default_proc # => nil
6923+
* Note that this entire section ("Hash Default"):
69376924
*
6938-
* You can set the default proc by calling Hash.new with a block or
6939-
* calling the method #default_proc=
6925+
* - Applies _only_ to methods #[], #dig, and #values_at.
6926+
* - Does _not_ apply to methods #assoc, #fetch, or #fetch_values,
6927+
* which are not affected by the default value or default proc.
69406928
*
6941-
* h = Hash.new { |hash, key| "Default value for #{key}" }
6942-
* h.default_proc.class # => Proc
6943-
* h.default_proc = proc { |hash, key| "Default value for #{key.inspect}" }
6944-
* h.default_proc.class # => Proc
6929+
* ===== Any-Key Default
6930+
*
6931+
* You can define an any-key default for a hash;
6932+
* that is, a value that will be returned for _any_ not-found key:
6933+
*
6934+
* - The value of #default_proc <i>must be</i> +nil+.
6935+
* - The value of #default (which may be any object, including +nil+)
6936+
* will be returned for a not-found key.
6937+
*
6938+
* You can set the default value when the hash is created with Hash.new and option +default_value+,
6939+
* or later with method #default=.
69456940
*
6946-
* When the default proc is set (i.e., not +nil+)
6947-
* and method #[] is called with with a non-existent key,
6948-
* #[] calls the default proc with both the +Hash+ object itself and the missing key,
6949-
* then returns the proc's return value:
6941+
* Note: although the value of #default may be any object,
6942+
* it may not be a good idea to use a mutable object.
69506943
*
6951-
* h = Hash.new { |hash, key| "Default value for #{key}" }
6952-
* h[:nosuch] # => "Default value for nosuch"
6944+
* ===== Per-Key Defaults
69536945
*
6954-
* Note that in the example above no entry for key +:nosuch+ is created:
6946+
* You can define a per-key default for a hash;
6947+
* that is, a Proc that will return a value based on the key itself.
69556948
*
6956-
* h.include?(:nosuch) # => false
6949+
* You can set the default proc when the hash is created with Hash.new and a block,
6950+
* or later with method #default_proc=.
69576951
*
6958-
* However, the proc itself can add a new entry:
6952+
* Note that the proc can modify +self+,
6953+
* but modifying +self+ in this way is not thread-safe;
6954+
* multiple threads can concurrently call into the default proc
6955+
* for the same key.
69596956
*
6960-
* synonyms = Hash.new { |hash, key| hash[key] = [] }
6961-
* synonyms.include?(:hello) # => false
6962-
* synonyms[:hello] << :hi # => [:hi]
6963-
* synonyms[:world] << :universe # => [:universe]
6964-
* synonyms.keys # => [:hello, :world]
6957+
* ==== \Method Default
69656958
*
6966-
* Note that setting the default proc will clear the default value and vice versa.
6959+
* For two methods, you can specify a default value for a not-found key
6960+
* that has effect only for a single method call
6961+
* (and not for any subsequent calls):
69676962
*
6968-
* Be aware that a default proc that modifies the hash is not thread-safe in the
6969-
* sense that multiple threads can call into the default proc concurrently for the
6970-
* same key.
6963+
* - For method #fetch, you can specify an any-key default:
6964+
* - For either method #fetch or method #fetch_values,
6965+
* you can specify a per-key default via a block.
69716966
*
69726967
* === What's Here
69736968
*

0 commit comments

Comments
 (0)