From: tieg.zaharia@... Date: 2015-11-18T17:16:45+00:00 Subject: [ruby-core:71560] [Ruby trunk - Feature #11710] [Open] [PATCH] Replace Set#merge with Set#merge! and make Set#merge non-mutating. Issue #11710 has been reported by Tieg Zaharia. ---------------------------------------- Feature #11710: [PATCH] Replace Set#merge with Set#merge! and make Set#merge non-mutating. https://bugs.ruby-lang.org/issues/11710 * Author: Tieg Zaharia * Status: Open * Priority: Normal * Assignee: Akinori MUSHA ---------------------------------------- The Set#merge method currently mutates its caller. I propose changing its behavior to non-mutating, and replace its current behavior with a mutating Set#merge! method. For example, the current behavior: ``` > s = Set.new [1,2,3] # => # > s.object_id # => 70125370250380 > s.merge([4,5,6]) # => # > s # => # > s.object_id # => 70125370250380 ``` Set describes itself as a hybrid of Array and Hash, but Hash#merge does not mutate its caller, and Set is implemented on top of Hash as well. Hash has a merge! method that can mutate instead: ``` > h = {a: 1, b: 2} # => {:a=>1, :b=>2} > h.object_id # => 70125369896320 > h.merge({c: 3}) # => {:a=>1, :b=>2, :c=>3} > h # => {:a=>1, :b=>2} irb(main):015:0> h.object_id # => 70125369896320 ``` We were taken by surprise with the existing behavior of Set#merge, especially since Set follows the bang pattern of mutating/non-mutating method names (e.g. collect!, reject!, select!, flatten!) I noticed this has been [suggested before](https://bugs.ruby-lang.org/issues/5185), but was hoping it might be possible as a breaking change for 2.3.0? ---Files-------------------------------- non_mutating_set_merge_method.diff (1.47 KB) -- https://bugs.ruby-lang.org/