Reputation: 14056
Using irb
, we can list methods for particular object by doing following:
"Name".methods
But if I want to know how many parameters are needed for a particular method, how can I achieve this? I mean is there any way (by hitting some command on irb), we can get number of parameters for particular method (instead of referring to docs)?
.methods
returns only method names, not list of parameters for method.
Upvotes: 28
Views: 9311
Reputation: 959
There is one special case when you can't use the approach defined in other answers. It's about the method #initialize
of some class that does not yet have an instance.
If you'll try Method#arity
for SomeClass::new
, you'll always get -1:
class SomeClass
def initialize(a, b)
end
end
SomeClass.method(:new).arity # => -1
You need an instance of your class SomeClass
to use Method#arity
on it, but you can't get this instance because you yet don't know how many arguments need SomeClass::new
.
Fortunately, this problem can be solved. Just use instance_method(:initialize)
on SomeClass
and you will get the desired result:
class SomeClass
def initialize(a, b)
end
end
SomeClass.instance_method(:initialize).arity # => 2
By the way, you can use this approach for any instance method of some class without creating an instance of this class. For example, the following two lines will give the same result:
"string".method(:strip).arity # => 0
String.instance_method(:strip).arity # => 0
Upvotes: 3
Reputation: 19228
You can use the method Method#arity
:
"string".method(:strip).arity
# => 0
From the Ruby documentation:
Returns an indication of the number of arguments accepted by a method. Returns a nonnegative integer for methods that take a fixed number of arguments. For Ruby methods that take a variable number of arguments, returns -n-1, where n is the number of required arguments. For methods written in C, returns -1 if the call takes a variable number of arguments.
So, for example:
# Variable number of arguments, one is required
def foo(a, *b); end
method(:foo).arity
# => -2
# Variable number of arguments, none required
def bar(*a); end
method(:bar).arity
# => -1
# Accepts no argument, implemented in C
"0".method(:to_f).arity
# => 0
# Variable number of arguments (0 or 1), implemented in C
"0".method(:to_i).arity
# => -1
Update I've just discovered the exitence of Method#parameters
, it could be quite useful:
def foo(a, *b); end
method(:foo).parameters
# => [[:req, :a], [:rest, :b]]
Upvotes: 44
Reputation: 2405
You can use arity
Returns an indication of the number of arguments accepted by a method. Returns a nonnegative integer for methods that take a fixed number of arguments. For Ruby methods that take a variable number of arguments, returns -n-1, where n is the number of required arguments. For methods written in C, returns -1 if the call takes a variable number of arguments.
Example from ruby-doc
class C
def one; end
def two(a); end
def three(*a); end
def four(a, b); end
def five(a, b, *c); end
def six(a, b, *c, &d); end
end
c = C.new
c.method(:one).arity #=> 0
c.method(:two).arity #=> 1
c.method(:three).arity #=> -1
c.method(:four).arity #=> 2
c.method(:five).arity #=> -3
c.method(:six).arity #=> -3
Upvotes: 9