From: merch-redmine@... Date: 2020-09-21T21:54:39+00:00 Subject: [ruby-core:100065] [Ruby master Bug#17181] Hash with default block breaks after transform values Issue #17181 has been updated by jeremyevans0 (Jeremy Evans). Thanks for the report. I can confirm the issue. It looks like `rb_hash_transform_values` is missing `COPY_DEFAULT(result, hash);`. I'll update the tests and submit a PR to fix this tonight. ---------------------------------------- Bug #17181: Hash with default block breaks after transform values https://bugs.ruby-lang.org/issues/17181#change-87615 * Author: crashtech (Carlos Silva) * Status: Open * Priority: Normal * ruby -v: ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-linux] * Backport: 2.5: UNKNOWN, 2.6: UNKNOWN, 2.7: UNKNOWN ---------------------------------------- I found a problem with Hashes that uses a default proc. ```ruby x = Hash.new { |h, k| h[k] = [] } x[:a] << 1 x[:b] << 2 y = x.transform_values { |arr| arr.map(&:next) } y[:c] << 3 ``` I expected that even after transforming the values I could add a new key, but the result is weird. ```ruby y[:c] # This returns the default_proc Proc object y[:c] << 3 # Since it is not the newly created array, it breaks with TypeError (callable object is expected) Y[:c].call # When tried to call the Proc I found out that the 'h' is nil, meaning that it doesn't have the reference to the hash anymore ``` It should work by just creating the new array on that hey and pushing the new value ```ruby y[:c] << 3 # Now y should be {:a=>[2], :b=>[3], :c=>[3]} ``` -- https://bugs.ruby-lang.org/ Unsubscribe: