-
-
Save Papierkorb/60e75eef306141cf2ff9 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env ruby | |
class Node | |
def value | |
raise "Missing implementation for #{self.class.name}#value" | |
end | |
end | |
class ValueNode < Node | |
attr_reader :value | |
def initialize(value) | |
@value = value | |
end | |
end | |
class BinaryNode < Node | |
attr_reader :left, :right | |
def initialize(left, right) | |
@left = left | |
@right = right | |
end | |
end | |
class Sum < BinaryNode | |
def value | |
left.value + right.value | |
end | |
end | |
class Multiply < BinaryNode | |
def value | |
left.value * right.value | |
end | |
end | |
puts Sum.new(ValueNode.new(3), Multiply.new(ValueNode.new(4), ValueNode.new(5))).value # 3 + (4 * 5) = 23 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment