Skip to content

Instantly share code, notes, and snippets.

@skinnyjames
Last active May 31, 2021 18:01
Show Gist options
  • Save skinnyjames/c949ae6452419b86761f42f477762912 to your computer and use it in GitHub Desktop.
Save skinnyjames/c949ae6452419b86761f42f477762912 to your computer and use it in GitHub Desktop.
valueobject.rb
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)
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