Skip to content
  • Ufuk Kayserilioglu's avatar
    99cee857
    Add copy with changes functionality for Data objects (#6766) · 99cee857
    Ufuk Kayserilioglu authored
    
    
    Implements [Feature #19000]
    
    This commit adds copy with changes functionality for `Data` objects
    using a new method `Data#with`.
    
    Since Data objects are immutable, the only way to change them is by
    creating a copy. This PR adds a `with` method for `Data` class instances
    that optionally takes keyword arguments.
    
    If the `with` method is called with no arguments, the behaviour is the
    same as the `Kernel#dup` method, i.e. a new shallow copy is created
    with no field values changed.
    
    However, if keyword arguments are supplied to the `with` method, then
    the copy is created with the specified field values changed. For
    example:
    ```ruby
        Point = Data.define(:x, :y)
        point = Point.new(x: 1, y: 2)
        point.with(x: 3) # => #<data Point x: 3, y: 2>
    ```
    
    Passing positional arguments to `with` or passing keyword arguments to
    it that do not correspond to any of the members of the Data class will
    raise an `ArgumentError`.
    
    Co-authored-by: default avatarAlan Wu <XrXr@users.noreply.github.com>
    99cee857
    Add copy with changes functionality for Data objects (#6766)
    Ufuk Kayserilioglu authored
    
    
    Implements [Feature #19000]
    
    This commit adds copy with changes functionality for `Data` objects
    using a new method `Data#with`.
    
    Since Data objects are immutable, the only way to change them is by
    creating a copy. This PR adds a `with` method for `Data` class instances
    that optionally takes keyword arguments.
    
    If the `with` method is called with no arguments, the behaviour is the
    same as the `Kernel#dup` method, i.e. a new shallow copy is created
    with no field values changed.
    
    However, if keyword arguments are supplied to the `with` method, then
    the copy is created with the specified field values changed. For
    example:
    ```ruby
        Point = Data.define(:x, :y)
        point = Point.new(x: 1, y: 2)
        point.with(x: 3) # => #<data Point x: 3, y: 2>
    ```
    
    Passing positional arguments to `with` or passing keyword arguments to
    it that do not correspond to any of the members of the Data class will
    raise an `ArgumentError`.
    
    Co-authored-by: default avatarAlan Wu <XrXr@users.noreply.github.com>
Loading