Hi I am trying to implement this Common Lisp Macro in Ruby 2.0.0-p247:
(defmacro binary-cmp (a b &key (test-func '>))
`(funcall #',test-func ,a ,b))
A binary test function that takes 2 arguments and 1 keyword argument test_func
, and the test_func
defaults to gt
proc.
gt = -> (a, b) { a > b }
lt = -> (a, b) { a < b }
equal = -> (a, b) { a == b }
def binary_cmp (a, b, test_func: gt)
test_func.(a, b)
end
But this won't work, because in binary_cmp
cannot see the outside: gt
.
How should I do to accomplish this? Is it possible? Or there is a common practice to do it? Thank you very much.
EDIT:
The reason why I need keyword argument is that my parameter list has 5 parameters and maybe the user only need the default test function (say lt
), or maybe someone wanna use (gt
) as default.