Project

General

Profile

Actions

Bug #17181

closed

Hash with default block breaks after transforming values

Added by crashtech (Carlos Silva) over 3 years ago. Updated over 3 years ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-linux]
[ruby-core:100062]

Description

I found a problem with Hashes that use a default proc.

x = Hash.new { |h, k| h[k] = [] }
x[:a] << 1
x[:b] << 2

I expected that transforming the values as follows:

y = x.transform_values { |arr| arr.map(&:next) }

should create a new array, and I should still be able to push a new element into it as follows:

y[:c] << 3
y # => {:a=>[2], :b=>[3], :c=>[3]}

But the result is weird.

y[:c] << 3 # >> TypeError (callable object is expected)
y[:c] # => the default_proc Proc object
Y[:c].call # 'h' is nil, meaning that it doesn't have the reference to the hash anymore
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0