# Confusion with Proc#arity

**URL:** <https://rubytalk.org/t/confusion-with-proc-arity/70233>\
**Category:** ruby-talk\
**Created:** [8 July 2014 17:48 UTC](https://rubytalk.org/t/confusion-with-proc-arity/70233 "2014-07-08T17:48:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arup\_Rakshit1](https://avatars.discourse-cdn.com/v4/letter/a/c6cbf5/32.png) [@Arup\_Rakshit1](https://rubytalk.org/u/Arup_Rakshit1)\
**Post date:** [8 July 2014 17:48 UTC](https://rubytalk.org/t/confusion-with-proc-arity/70233/1 "2014-07-08T17:48:08Z")

</div>

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 Rakshit
> 
> 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

---

<div class="post-metadata">

**Author:** ![Eric\_Christopherson](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/eric_christopherson/32/1849_2.png) [@Eric\_Christopherson](https://rubytalk.org/u/Eric_Christopherson)\
**Post date:** [8 July 2014 20:53 UTC](https://rubytalk.org/t/confusion-with-proc-arity/70233/2 "2014-07-08T20:53:16Z")

</div>

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.

> **···**
>
> On Wed, Jul 09, 2014, Arup Rakshit wrote:
> 
> > 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
