True arity

Discussion in 'Ruby' started by Daniel DeLorme, May 29, 2007.

  1. Is there a way to get the *maximum* number of arguments that a method
    can receive? Method#arity only gives the lower bound...

    Daniel
    Daniel DeLorme, May 29, 2007
    #1
    1. Advertising

  2. Daniel DeLorme

    Robert Dober Guest

    On 5/29/07, Daniel DeLorme <> 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?

    Cheers
    Robert
    >
    > Daniel
    >
    >



    --
    You see things; and you say Why?
    But I dream things that never were; and I say Why not?
    -- George Bernard Shaw
    Robert Dober, May 29, 2007
    #2
    1. Advertising

  3. Robert Dober wrote:
    > On 5/29/07, Daniel DeLorme <> 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
    Daniel DeLorme, May 29, 2007
    #3
  4. 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 <> wrote:
    > Robert Dober wrote:
    > > On 5/29/07, Daniel DeLorme <> 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
    >
    >
    Michael Fellinger, May 30, 2007
    #4
  5. Daniel DeLorme

    Robert Dober Guest

    On 5/29/07, Daniel DeLorme <> wrote:
    > Robert Dober wrote:
    > > On 5/29/07, Daniel DeLorme <> 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)

    I know I missed something :(, indeed a good question, thx for
    explaining to a dummy ;).
    > 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
    >
    >

    Cheers
    Robert

    --
    You see things; and you say Why?
    But I dream things that never were; and I say Why not?
    -- George Bernard Shaw
    Robert Dober, May 30, 2007
    #5
  6. Daniel DeLorme wrote:
    > 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)


    It could be done, but the current Ruby implementations don't provide
    this information. Of course, all of them could...we know how many
    required arguments there are, we know how many optional arguments there
    are, and we know if there's a "rest" arg or not.

    Maximum =

    ...base arity if no optional args or rest arg
    ...base arity plus optional arg count if no rest arg
    ...infinity if rest arg in any case

    Minimum = ...well you know it, because we have this today.

    I guess the question would be whether this is useful enough information
    to warrant a semantic change in "arity" representation.

    - Charlie
    Charles Oliver Nutter, May 30, 2007
    #6
  7. Charles Oliver Nutter wrote:
    > I guess the question would be whether this is useful enough information
    > to warrant a semantic change in "arity" representation.


    It can be useful in some cases and it is *certainly* cleaner than the
    wondrous hack that is a negative arity, but I'm never very fond of
    backwards compatibility breakage. I'd be more in favor of introducing a
    new method (e.g. 'arityrange') that would return a Range object. (please
    pretty please mr. core developpers)

    IMHO, Range objects are underused. MatchData#offset would be so much
    nicer if it returned (a...b) instead of [a,b]

    just my 2¥

    Daniel
    Daniel DeLorme, May 30, 2007
    #7
  8. Daniel DeLorme

    Robert Dober Guest

    On 5/30/07, Daniel DeLorme <> wrote:
    > Charles Oliver Nutter wrote:
    > > I guess the question would be whether this is useful enough information
    > > to warrant a semantic change in "arity" representation.

    >
    > It can be useful in some cases and it is *certainly* cleaner than the
    > wondrous hack that is a negative arity, but I'm never very fond of
    > backwards compatibility breakage. I'd be more in favor of introducing a
    > new method (e.g. 'arityrange') that would return a Range object. (please
    > pretty please mr. core developpers)
    >
    > IMHO, Range objects are underused. MatchData#offset would be so much
    > nicer if it returned (a...b) instead of [a,b]
    >
    > just my 2=A5

    that is clever Daniel ;)

    but seriously I just wanted to backup your idea which seems pretty
    good, what about an RCR?
    And yes, by all means, use a range, look at this pretty idiom which would r=
    esult

    raise ArgumentError unless method.arityrange =3D=3D=3D n

    Cheers
    Robert
    >
    > Daniel
    >
    >



    --=20
    You see things; and you say Why?
    But I dream things that never were; and I say Why not?
    -- George Bernard Shaw
    Robert Dober, May 30, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Howard Gardner
    Replies:
    4
    Views:
    344
    Howard Gardner
    Jul 20, 2006
  2. Michael Feathers
    Replies:
    3
    Views:
    282
    Roland Pibinger
    Aug 4, 2006
  3. bdb112
    Replies:
    45
    Views:
    1,321
    jazbees
    Apr 29, 2009
  4. ðÅÔÒÏ× áÌÅËÓÁÎÄÒ

    Why the expression "(1)" is not an one-arity tuple, but int ?

    ðÅÔÒÏ× áÌÅËÓÁÎÄÒ, Dec 4, 2009, in forum: Python
    Replies:
    0
    Views:
    265
    ðÅÔÒÏ× áÌÅËÓÁÎÄÒ
    Dec 4, 2009
  5. Jeff Mitchell

    getting the arity

    Jeff Mitchell, Aug 28, 2003, in forum: Ruby
    Replies:
    1
    Views:
    85
    Harry Ohlsen
    Aug 28, 2003
Loading...

Share This Page