# How to distinguish blocks of certain arity in 1.8

**URL:** <https://www.ruby-forum.com/t/how-to-distinguish-blocks-of-certain-arity-in-1-8/166203>\
**Category:** Ruby\
**Created:** [April 22, 2009, 6:45pm UTC](https://www.ruby-forum.com/t/how-to-distinguish-blocks-of-certain-arity-in-1-8/166203 "2009-04-22T18:45:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 22, 2009, 6:45pm UTC](https://www.ruby-forum.com/t/how-to-distinguish-blocks-of-certain-arity-in-1-8/166203/1 "2009-04-22T18:45:56Z")

</div>

Hi all,

I’d like to distinguish the two following blocks by means of their  
arity, but in Ruby 1.8 I don’t see how to do this as Proc#arity  
returns the same value.

lambda {}.arity =\> -1  
lambda {|\*a|}.arity =\> -1

This is not the case in 1.9 as the semantics of the first one has  
changed, and now it’s equivalent to lambda {||}. Both return 1 and  
complain when the number of arguments passed is not correct.

Any ideas?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 22, 2009, 7:30pm UTC](https://www.ruby-forum.com/t/how-to-distinguish-blocks-of-certain-arity-in-1-8/166203/2 "2009-04-22T19:30:32Z")

</div>

I respond to myself. I’ve done this, admittedly in a bit ugly way, by  
using parse\_tree.to\_ruby and matching with |\*.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 22, 2009, 7:34pm UTC](https://www.ruby-forum.com/t/how-to-distinguish-blocks-of-certain-arity-in-1-8/166203/3 "2009-04-22T19:34:04Z")

</div>

unknown wrote:

> Hi all,
> 
> I’d like to distinguish the two following blocks by means of their  
> arity, but in Ruby 1.8 I don’t see how to do this as Proc#arity  
> returns the same value.
> 
> lambda {}.arity =\> -1  
> lambda {|\*a|}.arity =\> -1

I suppose the answer is that they have the same arity. These are all  
legal

lambda {}.call  
lambda {}.call :hello  
lambda {}.call :hello, :world

lambda { |\*a| }.call  
lambda { |\*a| }.call :hello  
lambda { |\*a| }.call :hello, :world

From the caller’s point of view, I see nothing which would distinguish  
one from the other.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 24, 2009, 7:31am UTC](https://www.ruby-forum.com/t/how-to-distinguish-blocks-of-certain-arity-in-1-8/166203/4 "2009-04-24T07:31:37Z")

</div>

The Higgs bozo wrote:

> unknown wrote:
> 
> > Hi all,
> > 
> > I’d like to distinguish the two following blocks by means of their  
> > arity, but in Ruby 1.8 I don’t see how to do this as Proc#arity  
> > returns the same value.
> > 
> > lambda {}.arity =\> -1  
> > lambda {|\*a|}.arity =\> -1
> 
> I suppose the answer is that they have the same arity. These are all  
> legal
> 
> lambda {}.call  
> lambda {}.call :hello  
> lambda {}.call :hello, :world
> 
> lambda { |\*a| }.call  
> lambda { |\*a| }.call :hello  
> lambda { |\*a| }.call :hello, :world
> 
> From the caller’s point of view, I see nothing which would distinguish  
> one from the other.

According the ri documentation

## renard$ ri Proc#arity ------------------------------------------------------------- Proc#arity prc.arity -\> fixnum

```
 Returns the number of arguments that would not be ignored. If the
 block is declared to take no arguments, returns 0. If the block is
 known to take exactly n arguments, returns n. If the block has
 optional arguments, return -n-1, where n is the number of
 mandatory arguments. A proc with no argument declarations is the
 same a block declaring || as its arguments.

    Proc.new {}.arity #=> 0
    Proc.new {||}.arity #=> 0
    Proc.new {|a|}.arity #=> 1
    Proc.new {|a,b|}.arity #=> 2
    Proc.new {|a,b,c|}.arity #=> 3
    Proc.new {|*a|}.arity #=> -1
    Proc.new {|a,*b|}.arity #=> -2

```

Note that Proc.new().arity should return a 0, however it returns -1  
!!!

renard$ irb --simple-prompt

> > Proc.new {}.arity  
> > =\> -1
> 
> >

I would consider that to be a bug

The ri documentation states that Proc.new {}.arity ande Proc.new{||} are  
equivalent. Proc.new{||}.arity  
does return a 0.

> > Proc.new {||}.arity  
> > =\> 0

lambda {}.arity has the same bug. Thus the OP should use lambda {||}

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [April 24, 2009, 8:06am UTC](https://www.ruby-forum.com/t/how-to-distinguish-blocks-of-certain-arity-in-1-8/166203/5 "2009-04-24T08:06:30Z")

</div>

Hi,

At Fri, 24 Apr 2009 14:31:41 +0900,  
Bernard K. wrote in [ruby-talk:334852]:

> Note that Proc.new().arity should return a 0, however it returns -1  
> !!!

It was a miss in rdoc which has been fixed already.
