Skip to content

Instantly share code, notes, and snippets.

@mazur
Created June 9, 2011 19:17
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 mazur/1017483 to your computer and use it in GitHub Desktop.
Save mazur/1017483 to your computer and use it in GitHub Desktop.
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