Project

General

Profile

Actions

Bug #16457

closed

Invisible keys on hash when defining hash with Hash.new({})

Added by Farhad (Farhad Eyvazli) over 4 years ago. Updated over 4 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
2.6.3
[ruby-core:96512]

Description

When using Hash.new() to initialize a hash, we all know for undefined it will return specific value which sent as a parameter to the ```Hash.new``
But when doing something like that keys get invisible

my_hash: Hash.new({})

my_hasy[:my_key] #=> {}
my_hash[:my_key].merge!(value: '')

my_hash.keys #=> []
my_hash.fetch(:my_key) #=> KeyError: key not found: :my_key

my_hash.dig(:my_key) #=> {:value=>""}
my_hash[:my_key] #=> {:value=>""}

Maybe it's normal behavior because, for each missing key, it initialize new empty has and merge it to that. But I'm not sure it can cause a memory leak or not when removing the main hash (:my_hash)

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0