Project

General

Profile

Actions

Feature #12719

closed

`Struct#merge` for partial updates

Added by halogenandtoast (Matthew Mongeau) over 7 years ago. Updated over 2 years ago.

Status:
Feedback
Assignee:
-
Target version:
-
[ruby-core:77125]

Description

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:

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) struct_merge.patch halogenandtoast (Matthew Mongeau), 09/05/2016 02:55 AM
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0