How to distinguish blocks of certain arity in 1.8

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.

I respond to myself. I’ve done this, admittedly in a bit ugly way, by
using parse_tree.to_ruby and matching with |*.

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.

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 {||}

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.