getting the arity

Discussion in 'Ruby' started by Jeff Mitchell, Aug 28, 2003.

  1. Hi,

    I would like to get the arity of a method. However, arity is a method
    of Method, and I can only get a Method from the method method of Object
    (phew!).

    So it appears impossible to get the arity of a method in a class
    without making an instance of that class. Is this true?

    A hypothetical use would be to print out all classes with their arity
    at a given time during execution (and making dummy instances could
    cause unwanted side-effects).

    -qs

    __________________________________
    Do you Yahoo!?
    Yahoo! SiteBuilder - Free, easy-to-use web site design software
    http://sitebuilder.yahoo.com
     
    Jeff Mitchell, Aug 28, 2003
    #1
    1. Advertising

  2. Jeff Mitchell

    Harry Ohlsen Guest

    Jeff Mitchell wrote:

    > Hi,
    >
    > I would like to get the arity of a method. However, arity is a method
    > of Method, and I can only get a Method from the method method of Object
    > (phew!).
    >
    > So it appears impossible to get the arity of a method in a class
    > without making an instance of that class. Is this true?
    >
    > A hypothetical use would be to print out all classes with their arity
    > at a given time during execution (and making dummy instances could
    > cause unwanted side-effects).


    Here's a starting point. Note that, of course, you get a lot more methods than you bargained for, because it will show you all the inherited methods, too.

    Hope it helps.

    ---------------

    def dump_methods(klass)
    klass.methods.each do |name|
    method = Fred.method(name)

    puts "#{name} arity #{method.arity}"
    end
    end

    class Fred
    def f(a, b, c)
    end

    def g(x)
    end
    end

    dump_methods Fred
     
    Harry Ohlsen, Aug 28, 2003
    #2
    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:
    357
    Howard Gardner
    Jul 20, 2006
  2. Michael Feathers
    Replies:
    3
    Views:
    286
    Roland Pibinger
    Aug 4, 2006
  3. ðÅÔÒÏ× áÌÅËÓÁÎÄÒ

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

    ðÅÔÒÏ× áÌÅËÓÁÎÄÒ, Dec 4, 2009, in forum: Python
    Replies:
    0
    Views:
    273
    ðÅÔÒÏ× áÌÅËÓÁÎÄÒ
    Dec 4, 2009
  4. Simon Strandgaard
    Replies:
    4
    Views:
    109
    Dan Doel
    Oct 29, 2003
  5. Paul Brannan
    Replies:
    2
    Views:
    115
    Yukihiro Matsumoto
    Nov 27, 2003
Loading...

Share This Page