[ruby-core:77129] [Ruby trunk Feature#12719] `Struct#|` for partial updates
From:
halogenandtoast@...
Date:
2016-09-02 13:11:21 UTC
List:
ruby-core #77129
Issue #12719 has been updated by Matthew Mongeau.
File struct_merge.patch added
As an alternative since the `|` syntax might get shot down. Here's a patch adding a merge function instead:
~~~ ruby
Point = Struct.new(:x, :y)
p = Point.new(1, 2)
p2 = p.merge(y: 4)
p3 = p2.merge(x: 10)
puts p.inspect # => #<struct Point x=1, y=2>
puts p2.inspect # => #<struct Point x=1, y=4>
puts p3.inspect # => #<struct Point x=10, y=4>
~~~
----------------------------------------
Feature #12719: `Struct#|` for partial updates
https://bugs.ruby-lang.org/issues/12719#change-60347
* Author: Matthew Mongeau
* Status: Open
* Priority: Normal
* Assignee:
----------------------------------------
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 | { y: 4 }
p3 = p2 | { x: 10 }
puts p.inspect # => #<struct Point x=1, y=2>
puts p2.inspect # => #<struct Point x=1, y=4>
puts p3.inspect # => #<struct Point x=10, y=4>
~~~
---Files--------------------------------
struct_update.patch (2.04 KB)
struct_merge.patch (2.04 KB)
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>