From: ruby-core@... Date: 2019-05-07T18:56:46+00:00 Subject: [ruby-core:92591] [Ruby trunk Bug#15834] Mutating the result of SortedSet.to_a mutates the original set, potentially violating the set's guarantees Issue #15834 has been updated by marcandre (Marc-Andre Lafortune). Assignee set to knu (Akinori MUSHA) ---------------------------------------- Bug #15834: Mutating the result of SortedSet.to_a mutates the original set, potentially violating the set's guarantees https://bugs.ruby-lang.org/issues/15834#change-77952 * Author: viko (Viko Viko) * Status: Open * Priority: Normal * Assignee: knu (Akinori MUSHA) * Target version: * ruby -v: ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin18] * Backport: 2.4: UNKNOWN, 2.5: UNKNOWN, 2.6: UNKNOWN ---------------------------------------- ``` ruby require 'set' set = SortedSet[1, 2, 3] set.to_a << -1 set.to_a << 3 set.each do |x| puts x end puts set.add -2 set.each do |x| puts x end ``` ``` 1 2 3 -1 3 -2 1 2 3 ``` As can be seen, this breaks SortedSet's guarantee of ordering *and* Set's guarantee of uniqueness, besides being strange behaviour. Mutating the set normally undoes the changes. This bug only seems to exist for SortedSet, and not for Set itself. Workaround: `dup`. -- https://bugs.ruby-lang.org/ Unsubscribe: