Project

General

Profile

Feature #12719

Updated by halogenandtoast (Matthew Mongeau) about 8 years ago

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) p | { y: 4 } 
 p3 = p2.merge(x: 10) 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> 

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

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

Back