[ruby-core:112477] [Ruby master Bug#19448] [Hash] Using Set as default value

Issue #19448 has been reported by bobanj (Boban Jovanoski). ---------------------------------------- Bug #19448: [Hash] Using Set as default value https://bugs.ruby-lang.org/issues/19448 * Author: bobanj (Boban Jovanoski) * Status: Open * Priority: Normal * ruby -v: ruby 3.1.3p185 (2022-11-24 revision 1a6b16756e) [x86_64-darwin22] * Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN ---------------------------------------- When using a hash and set is used as a default value, the keys method for the hash does not return expected values. A workaround for this is provided in the attachment. ---Files-------------------------------- bug.rb (382 Bytes) -- https://bugs.ruby-lang.org/

Issue #19448 has been updated by byroot (Jean Boussier). Status changed from Open to Rejected This isn't a bug and is documented in `Hash.new` https://docs.ruby-lang.org/en/3.2/Hash.html#class-Hash-label-Default+Values
Note that the default value is used without being duplicated. It is not advised to set the default value to a mutable object:
```ruby synonyms = Hash.new([]) synonyms[:hello] # => [] synonyms[:hello] << :hi # => [:hi], but this mutates the default! synonyms.default # => [:hi] synonyms[:world] << :universe synonyms[:world] # => [:hi, :universe], oops synonyms.keys # => [], oops ```
To use a mutable object as default, it is recommended to use a default proc
---------------------------------------- Bug #19448: [Hash] Using Set as default value https://bugs.ruby-lang.org/issues/19448#change-101920 * Author: bobanj (Boban Jovanoski) * Status: Rejected * Priority: Normal * ruby -v: ruby 3.1.3p185 (2022-11-24 revision 1a6b16756e) [x86_64-darwin22] * Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN ---------------------------------------- When using a hash and set is used as a default value, the keys method for the hash does not return expected values. A workaround for this is provided in the attachment. ---Files-------------------------------- bug.rb (382 Bytes) -- https://bugs.ruby-lang.org/

Issue #19448 has been updated by Hanmac (Hans Mackowiak). That's is known limitation when using Hash default value Also your problem is that `<<` would alter the same object you put into as default object meaning: ```ruby h = Hash.new(Set.new) h[:a] << 1 h[:b] << 2 p h[:c] #=> Set[1,2] ``` ---------------------------------------- Bug #19448: [Hash] Using Set as default value https://bugs.ruby-lang.org/issues/19448#change-101921 * Author: bobanj (Boban Jovanoski) * Status: Rejected * Priority: Normal * ruby -v: ruby 3.1.3p185 (2022-11-24 revision 1a6b16756e) [x86_64-darwin22] * Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN ---------------------------------------- When using a hash and set is used as a default value, the keys method for the hash does not return expected values. A workaround for this is provided in the attachment. ---Files-------------------------------- bug.rb (382 Bytes) -- https://bugs.ruby-lang.org/

Issue #19448 has been updated by sawa (Tsuyoshi Sawada). @byroot (Jean Boussier), I think your comment is misleading if not irrelevant. The issue's point is that, a key-value pair is not stored in the hash just by calling it. The only relevant part in the example you cited is: ```ruby synonyms.keys # => [], oops ``` which is indeed not a bug.
To use a mutable object as default, it is recommended to use a default proc
is irrelevant/incomplete as a response to this issue because: ```ruby h = Hash.new{Set.new} ``` will not help. You have to explicitly store the key-value pair as in: ```ruby h = Hash.new{|hash, key| hash[key] = Set.new} ``` ---------------------------------------- Bug #19448: [Hash] Using Set as default value https://bugs.ruby-lang.org/issues/19448#change-101928 * Author: bobanj (Boban Jovanoski) * Status: Rejected * Priority: Normal * ruby -v: ruby 3.1.3p185 (2022-11-24 revision 1a6b16756e) [x86_64-darwin22] * Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN ---------------------------------------- When using a hash and set is used as a default value, the keys method for the hash does not return expected values. A workaround for this is provided in the attachment. ---Files-------------------------------- bug.rb (382 Bytes) -- https://bugs.ruby-lang.org/

Issue #19448 has been updated by byroot (Jean Boussier). @sawa I wasn't planning to answer the question myself, only to point the author to the detailed documentation of how Hash defaults works, so they can answer their own question. ---------------------------------------- Bug #19448: [Hash] Using Set as default value https://bugs.ruby-lang.org/issues/19448#change-101931 * Author: bobanj (Boban Jovanoski) * Status: Rejected * Priority: Normal * ruby -v: ruby 3.1.3p185 (2022-11-24 revision 1a6b16756e) [x86_64-darwin22] * Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN, 3.2: UNKNOWN ---------------------------------------- When using a hash and set is used as a default value, the keys method for the hash does not return expected values. A workaround for this is provided in the attachment. ---Files-------------------------------- bug.rb (382 Bytes) -- https://bugs.ruby-lang.org/
participants (4)
-
bobanj (Boban Jovanoski)
-
byroot (Jean Boussier)
-
Hanmac (Hans Mackowiak)
-
sawa (Tsuyoshi Sawada)