Skip to content

Instantly share code, notes, and snippets.

@Papierkorb
Created December 8, 2015 21:49
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 Papierkorb/60e75eef306141cf2ff9 to your computer and use it in GitHub Desktop.
Save Papierkorb/60e75eef306141cf2ff9 to your computer and use it in GitHub Desktop.
#!/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