[#78633] ruby/spec needs help from CRuby committers — Benoit Daloze <eregontp@...>

Currently, ruby/spec is maintained mostly by individuals and enjoys the

13 messages 2016/12/13

[ruby-core:78658] [Ruby trunk Feature#12719] `Struct#merge` for partial updates

From: eregontp@...
Date: 2016-12-15 10:41:15 UTC
List: ruby-core #78658
Issue #12719 has been updated by Benoit Daloze.


Scala has "copy" for this purpose: some_case_class_object.copy(field: new_value)

----------------------------------------
Feature #12719: `Struct#merge` for partial updates
https://bugs.ruby-lang.org/issues/12719#change-62040

* Author: Matthew Mongeau
* Status: Feedback
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
Other languages have operators for performing partial updates on maps. I feel like Struct could be more useful if it provided an easy way of performing partial (or full) updates.

After the change you can do the following:

~~~ ruby
Point = Struct.new(:x, :y)

p = Point.new(1, 2)
p2 = p.merge(y: 4)
p3 = p2.merge(x: 10)

p.inspect     # => #<struct Point x=1, y=2>
p2.inspect   # => #<struct Point x=1, y=4>
p3.inspect   # => #<struct Point x=10, y=4>

p.merge!("x" => 9)

p.inspect    # => #<struct Point x=9, y=2>
~~~

---Files--------------------------------
struct_merge.patch (2.93 KB)


-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>

In This Thread

Prev Next