Skip to content

Instantly share code, notes, and snippets.

@Thomascountz
Last active August 22, 2022 08:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Thomascountz/4d63cc223455150afc8094d2542355a9 to your computer and use it in GitHub Desktop.
Save Thomascountz/4d63cc223455150afc8094d2542355a9 to your computer and use it in GitHub Desktop.
Addition operation on a Value object
graph LR
    A["A | data: 1.0"]
    APlus(("+"))
    B["B | data: 2.0"]
    C["C | data: 3.0"]
    A --> APlus
    B --> APlus
    APlus --> C
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