[DOC] Tweaks for Hash#dig

This commit is contained in:
BurdetteLamar 2025-02-17 12:18:56 -06:00 committed by Peter Zhu
parent 266088a85a
commit 36f69d5b69
Notes: git 2025-02-18 00:09:50 +00:00

22
hash.c
View file

@ -4619,29 +4619,35 @@ rb_hash_any_p(int argc, VALUE *argv, VALUE hash)
* call-seq: * call-seq:
* dig(key, *identifiers) -> object * dig(key, *identifiers) -> object
* *
* Finds and returns the object in nested objects * Finds and returns an object found in nested objects,
* that is specified by +key+ and +identifiers+. * as specified by +key+ and +identifiers+.
*
* The nested objects may be instances of various classes. * The nested objects may be instances of various classes.
* See {Dig Methods}[rdoc-ref:dig_methods.rdoc]. * See {Dig Methods}[rdoc-ref:dig_methods.rdoc].
* *
* Nested Hashes: * Nested hashes:
*
* h = {foo: {bar: {baz: 2}}} * h = {foo: {bar: {baz: 2}}}
* h.dig(:foo) # => {bar: {baz: 2}} * h.dig(:foo) # => {bar: {baz: 2}}
* h.dig(:foo, :bar) # => {baz: 2} * h.dig(:foo, :bar) # => {baz: 2}
* h.dig(:foo, :bar, :baz) # => 2 * h.dig(:foo, :bar, :baz) # => 2
* h.dig(:foo, :bar, :BAZ) # => nil * h.dig(:foo, :bar, :BAZ) # => nil
* *
* Nested Hashes and Arrays: * Nested hashes and arrays:
*
* 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 {hash default}[rdoc-ref:Hash@Hash+Default] * If no such object is found,
* for keys that are not present: * returns the {hash default}[rdoc-ref:Hash@Hash+Default]:
*
* h = {foo: {bar: [:a, :b, :c]}} * h = {foo: {bar: [:a, :b, :c]}}
* h.dig(:hello) # => nil * h.dig(:hello) # => nil
* h.default_proc = -> (hash, _key) { hash } * h.default_proc = -> (hash, _key) { hash }
* h.dig(:hello, :world) # => h * h.dig(:hello, :world)
* h.dig(:hello, :world, :foo, :bar, 2) # => :c * # => {:foo=>{:bar=>[:a, :b, :c]}}
*
* Related: {Methods for Fetching}[rdoc-ref:Hash@Methods+for+Fetching].
*/ */
static VALUE static VALUE