【发布时间】:2026-02-05 18:20:07
【问题描述】:
不确定是否有答案,但希望有人知道。我正在尝试获取 Ruby 1.8.7 中方法的可选参数的数量。 Method#arity 将不起作用,因为它返回 -n-1,其中 n 是方法所需参数的数量。我需要的是可选参数的数量?例如
def foo(a,b,c=4,d=3)
# ...
end
我如何确定有 2 个可选参数?请记住,这是 Ruby 1.8.7
更新
抱歉问题不清楚,在调用该方法之前我需要知道可选参数的数量。例如
method_def = self.instance_method(:foo)
# check for number of args
# call method if it meets some kind of criteria
【问题讨论】: