Confusion with Proc#arity

Hi,

I am having confusion with the output of Proc#arity

#arity calculation is being done by -n-1. Where n is the number of
required
argument.

Here is n is 1, so -2. Its okay

proc { |a, *b| }.arity # => -2

Here is also n is 1. But why 1, not -2 ?

proc { |a , b = 0| }.arity # => 1

Regards,
Arup R.

Debugging is twice as hard as writing the code in the first place.
Therefore,
if you write the code as cleverly as possible, you are, by definition,
not
smart enough to debug it.

–Brian Kernighan

On Wed, Jul 09, 2014, Arup R. wrote:

Here is also n is 1. But why 1, not -2 ?

proc { |a , b = 0| }.arity # => 1

The docs seem to be lacking. Perhaps part of the reason for the
discrepancy is because calling a proc, unlike calling a lambda, doesn’t
cause Ruby to check the number of parameters. If you were to code your
examples using lambdas instead of procs, you would find the docs’
explanation to fit the results.

I’m not exactly sure how arity is determined for procs, but from the
examples in the docs it appears that a proc no parameters, or only
optional parameters, gets 0; a proc with one mandatory parameter (with
or without optional parameters) gets 1; and I would guess it would
continue from 1 on up as mandatory parameters were increased. Apparently
procs are totally exempt from the “-n-1” rule given for lambdas.

As to why that is, I’m not sure. procs can be conceptualized as not
having any mandatory parameters, since they can be called without
error with any number of arguments.