Skip to content

Commit 9dab3c5

Browse files
[DOC] Mention Hash#default_proc= affects Proc object from Hash#to_proc
```ruby h = {foo: 0, bar: 1, baz: 2} proc = h.to_proc proc.call(:nosuch) # => nil h.default_proc = proc {'wow!'} # This affects `proc` immediately proc.call(:nosuch) # => 'wow!' ``` This behavior is a bit of surprise to me, and I could not find any mention to this behavior in current doc.
1 parent e0a7dad commit 9dab3c5

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-0
lines changed

hash.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5012,6 +5012,8 @@ hash_proc_call(RB_BLOCK_CALL_FUNC_ARGLIST(key, hash))
50125012
* proc.call(:foo) # => 0
50135013
* proc.call(:bar) # => 1
50145014
* proc.call(:nosuch) # => nil
5015+
* h.default_proc = proc { |hash, key| "Missing key: #{key}" } # This affect the existing proc object
5016+
* proc.call(:nosuch) # => "Missing key: #{nosuch}"
50155017
*
50165018
* Related: see {Methods for Converting}[rdoc-ref:Hash@Methods+for+Converting].
50175019
*/

0 commit comments

Comments
 (0)