Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to know how many parameters are needed for a method?

Tags:

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.

like image 375
Alpha Avatar asked Jul 11 '13 09:07

Alpha


People also ask

How many parameters does a method need?

5 parameters in a method is found in almost 2 code base out of 3 (61%). Reaching 6 parameters is below average, so the common sense that set the bar around 3 or 4, and “for sure, nothing beyond 6”, can be read on the actual coding. Methods with 10 arguments or more appear in less that 20% of projects.

How many parameters are there in Find () method?

Parameters for the find() method Here, are three parameters of the function String find() in Python: substring: The substring you want to search in the given string. start: (optional) The start value from where the search for substring will begin. By default, it is 0.

How many parameters are possible in a function?

This limit is determined by stacked frame size, which is set by OS. Theoretically you can set these max stack size to 8192 bits. Each variable takes up 32 bits then you could pass 256 parameters.

How many mandatory parameters should be passed in open method?

Syntax. This function accepts four parameters, but they are optional. You can also use this function without using the window keyword as shown below: open(URL, name, specs, replace)


2 Answers

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]]  
like image 73
toro2k Avatar answered Oct 19 '22 06:10

toro2k


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 
like image 36
revolver Avatar answered Oct 19 '22 05:10

revolver