graph LR
A["A | data: 1.0"]
APlus(("+"))
B["B | data: 2.0"]
C["C | data: 3.0"]
A --> APlus
B --> APlus
APlus --> C
Last active
August 22, 2022 08:23
-
-
Save Thomascountz/4d63cc223455150afc8094d2542355a9 to your computer and use it in GitHub Desktop.
Addition operation on a Value object
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 Value | |
attr_reader :data, :operation, :children | |
def initialize(data:, operation: nil, children: []) | |
@data = data | |
@operation = operation | |
@children = children | |
end | |
def +(other) | |
Value.new( | |
data: data + other.data, | |
operation: :+, | |
children: [self, other] | |
) | |
end | |
def to_s | |
"#<Value data: #{data}>" | |
end | |
end | |
puts Value.new(data: 1.0) + Value.new(data: 2.0) | |
#=> #<Value data: 3.0> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment