diff options
| author | BurdetteLamar <burdettelamar@yahoo.com> | 2025-02-18 15:10:28 -0600 |
|---|---|---|
| committer | Peter Zhu <peter@peterzhu.ca> | 2025-02-19 09:46:52 -0500 |
| commit | 273e35cdcc04c6c68066a963157896472a2d1eb3 (patch) | |
| tree | 6b2f24d52f5370448f93dda4c77d2334101c16f3 | |
| parent | a46997a8f70c3f4640c07c719de82b8d612354cf (diff) | |
[DOC] Tweaks for Hash#fetch
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/12780
| -rw-r--r-- | hash.c | 21 |
1 files changed, 12 insertions, 9 deletions
@@ -2143,21 +2143,24 @@ rb_hash_lookup(VALUE hash, VALUE key) * fetch(key, default_value) -> object * fetch(key) {|key| ... } -> object * - * Returns the value for the given +key+, if found. + * With no block given, returns the value for the given +key+, if found; + * * h = {foo: 0, bar: 1, baz: 2} - * h.fetch(:bar) # => 1 + * h.fetch(:bar) # => 1 * - * If +key+ is not found and no block was given, - * returns +default_value+: - * {}.fetch(:nosuch, :default) # => :default + * If the key is not found, returns +default_value+, if given, + * or raises KeyError otherwise: * - * If +key+ is not found and a block was given, - * yields +key+ to the block and returns the block's return value: - * {}.fetch(:nosuch) {|key| "No key #{key}"} # => "No key nosuch" + * h.fetch(:nosuch, :default) # => :default + * h.fetch(:nosuch) # Raises KeyError. + * + * With a block given, calls the block with +key+ and returns the block's return value: * - * Raises KeyError if neither +default_value+ nor a block was given. + * {}.fetch(:nosuch) {|key| "No key #{key}"} # => "No key nosuch" * * Note that this method does not use the values of either #default or #default_proc. + * + * Related: see {Methods for Fetching}[rdoc-ref:Hash@Methods+for+Fetching]. */ static VALUE |
