Skip to content

Instantly share code, notes, and snippets.

@mazur
Created June 9, 2011 19:19
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/1017489 to your computer and use it in GitHub Desktop.
Save mazur/1017489 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
macro def add_op(name, &op)
quote {
@ops ||= HashMap.new
@ops[`name`] = `op.body`
def `name`
IOperator(@ops[`name`])
end
}
end
add_op :add { |x, y| x + y }
add_op :sub { |x, y| x - y }
end
def calculate(op:IOperator)
op.run(4, 20)
end
op = Operators.new
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