Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > getting the arity

Reply
Thread Tools

getting the arity

 
 
Jeff Mitchell
Guest
Posts: n/a
 
      08-28-2003
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

 
Reply With Quote
 
 
 
 
Harry Ohlsen
Guest
Posts: n/a
 
      08-28-2003
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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why the expression "(1)" is not an one-arity tuple, but int ? Петров Александр Python 0 12-04-2009 12:17 PM
Detecting the arity of a constructor at compile time Michael Feathers C++ 3 08-04-2006 04:18 PM
overloading on the template parameter arity of a template templateparameter Howard Gardner C++ 4 07-19-2006 11:10 PM
arity of methods defined with define_method Paul Brannan Ruby 2 11-27-2003 05:41 PM
[RCR] #inject, #partition expand array if arity > 2 Simon Strandgaard Ruby 4 10-29-2003 10:17 PM



Advertisments