Michael Fellinger
5/30/2007 2:22:00 AM
arity is not able to show you all the information you need, default
parameter are a curse in this regard (or the format arity uses - a
simple integer is not enough to show all cases... even a range
wouldn't, since
(1..(1.0/0.0))
# 1..Infinity
won't tell you that much (i think)
I don't know of a really simple programmatic way to determine the
arity, ruby2ruby and manual parsing might help you there if you
_really_ really need it. Best would be to just raise and tell the user
he used the wrong arity. Or you could do a rundown, passing as many
parameters as possible and going down until it doesn't raise any more.
Please note that this post is full of bad practice and evil hacks :)
^ manveru
On 5/30/07, Daniel DeLorme <dan-ml@dan42.com> wrote:
> Robert Dober wrote:
> > On 5/29/07, Daniel DeLorme <dan-ml@dan42.com> wrote:
> >> Is there a way to get the *maximum* number of arguments that a method
> >> can receive? Method#arity only gives the lower bound...
> >
> > I am afraid I fail to understand, what would you define as the maximum?
> >
> > def a(a)... I would say the maximum is 1 which is the arity
> > def b(*b) arity is -1 but what would the maximum of parameters be?
> > def c(c,*d) arity = -2 but some question as above.
> >
> > The question is, what would you like to achieve?
>
> In the last 2 cases you give the maximum would be Infinity. But in a
> case like this:
> def foo(a, b=nil, c=nil)
> the maximum would be 3 but arity only gives me -2 which means "this
> method has 1 required argument". If I invoke the method like:
> foo(*args)
> I want to know for what size of args is this valid? It would be nice if
> arity returned a Range object (1..3)
>
> Daniel
>
>