Created
June 9, 2011 19:17
-
-
Save mazur/1017483 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
import java.util.HashMap | |
interface IOperator do | |
def run(x:int, y:int) | |
returns int | |
end | |
end | |
class Operators | |
def add | |
IOperator(@ops[:add]) | |
end | |
def sub | |
IOperator(@ops[:sub]) | |
end | |
def add_op(name:string, op:IOperator) | |
@ops ||= HashMap.new | |
@ops[name] = op | |
end | |
end | |
def calculate(op:IOperator) | |
op.run(4, 20) | |
end | |
op = Operators.new | |
op.add_op :add { |x, y| x + y } | |
op.add_op :sub { |x, y| x - y } | |
puts calculate op.add | |
puts calculate op.sub |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment