Skip to content

Commit 273e35c

Browse files
BurdetteLamarpeterzhu2118
authored andcommitted
[DOC] Tweaks for Hash#fetch
1 parent a46997a commit 273e35c

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

hash.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2143,21 +2143,24 @@ rb_hash_lookup(VALUE hash, VALUE key)
21432143
* fetch(key, default_value) -> object
21442144
* fetch(key) {|key| ... } -> object
21452145
*
2146-
* Returns the value for the given +key+, if found.
2146+
* With no block given, returns the value for the given +key+, if found;
2147+
*
21472148
* h = {foo: 0, bar: 1, baz: 2}
2148-
* h.fetch(:bar) # => 1
2149+
* h.fetch(:bar) # => 1
21492150
*
2150-
* If +key+ is not found and no block was given,
2151-
* returns +default_value+:
2152-
* {}.fetch(:nosuch, :default) # => :default
2151+
* If the key is not found, returns +default_value+, if given,
2152+
* or raises KeyError otherwise:
21532153
*
2154-
* If +key+ is not found and a block was given,
2155-
* yields +key+ to the block and returns the block's return value:
2156-
* {}.fetch(:nosuch) {|key| "No key #{key}"} # => "No key nosuch"
2154+
* h.fetch(:nosuch, :default) # => :default
2155+
* h.fetch(:nosuch) # Raises KeyError.
2156+
*
2157+
* With a block given, calls the block with +key+ and returns the block's return value:
21572158
*
2158-
* Raises KeyError if neither +default_value+ nor a block was given.
2159+
* {}.fetch(:nosuch) {|key| "No key #{key}"} # => "No key nosuch"
21592160
*
21602161
* Note that this method does not use the values of either #default or #default_proc.
2162+
*
2163+
* Related: see {Methods for Fetching}[rdoc-ref:Hash@Methods+for+Fetching].
21612164
*/
21622165

21632166
static VALUE

0 commit comments

Comments
 (0)