getting the arity

J

Jeff Mitchell

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
 
H

Harry Ohlsen

Jeff said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top