这就是我想要做的。
# DSL Commands
command :foo, :name, :age
command :bar, :name
# Defines methods
def foo(name, age)
# Do something
end
def bar(name)
# Do something
end
基本上,我需要一种通过 处理参数的方法define_method
,但我想要定义数量的参数而不是 arg 数组(即*args
)
这是我到目前为止所拥有的
def command(method, *args)
define_method(method) do |*args|
# Do something
end
end
# Which would produce
def foo(*args)
# Do something
end
def bar(*args)
# Do something
end
想法?