Last active
May 31, 2021 18:01
-
-
Save skinnyjames/c949ae6452419b86761f42f477762912 to your computer and use it in GitHub Desktop.
valueobject.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Point | |
attr_reader :x, :y | |
def self.clone_from(point, x: nil, y: nil) | |
new(x || point.x, y || point.y) | |
end | |
def initialize(x=0, y=0) | |
@x = x | |
@y = y | |
end | |
end | |
first_point = Point.new(x: 1, y: 2) | |
second_point = Point.clone_from(first_point, y: 3) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ValueObject | |
attr_reader :attribute1, :attribute2 | |
def self.clone_from(obj, attribute1: nil, attribute2: nil) | |
new(attribute1 || obj.attribute1, attribute2 || obj.attribute2) | |
end | |
def initialize(attribute1, attribute2) | |
@attribute1 = attribute1 | |
@attribute2 = attribute2 | |
end | |
end | |
a = ValueObject.new(1, 2) | |
b = ValueObject.clone_from(a, attribute2: 3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment