Not sure that there is an answer for this but hope somebody know. What I am trying is to get number of optional arguments for a Method in Ruby 1.8.7. Method#arity will not work since it returns -n-1, where n is number of required arguments of method. What I need is number of optional arguments? e.g.
def foo(a,b,c=4,d=3)
# ...
end
How can I identify that there are 2 optional arguments? Keep in mind this is Ruby 1.8.7
UPDATE
Apologies question was not clear, I need to know number of optional arguments before calling the method. e.g.
method_def = self.instance_method(:foo)
# check for number of args
# call method if it meets some kind of criteria
I don’t think you can. In fact, I think that was one of the reasons for the introduction of
{Proc, Method, UnboundMethod}#parametersin Ruby 1.9: