Project

General

Profile

Bug #17181

Updated by sawa (Tsuyoshi Sawada) over 3 years ago

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

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

 I expected that transforming the values as follows: 

 ```ruby 
 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: 

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

 But 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] Y[:c].call # => When tried to call the default_proc Proc object 
 Y[:c].call # 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]} 
 ```

Back