Forum: Ruby How to distinguish blocks of certain arity in 1.8

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
0408caacfbd9186ebd08387a61bb8734?d=identicon&s=25 unknown (Guest)
on 2009-04-22 18:45
(Received via mailing list)
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.
0408caacfbd9186ebd08387a61bb8734?d=identicon&s=25 unknown (Guest)
on 2009-04-22 19:30
(Received via mailing list)
I respond to myself. I've done this, admittedly in a bit ugly way, by
using parse_tree.to_ruby and matching with |*.
Beb77c4602c3cac7a12149431366ed11?d=identicon&s=25 The Higgs bozo (higgsbozo)
on 2009-04-22 19:34
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.
Feee221f9eb7818d90625ea141bfd60c?d=identicon&s=25 Bernard Kenik (bbiker)
on 2009-04-24 07:31
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 {||}
F1d6cc2b735bfd82c8773172da2aeab9?d=identicon&s=25 Nobuyoshi Nakada (nobu)
on 2009-04-24 08:06
(Received via mailing list)
Hi,

At Fri, 24 Apr 2009 14:31:41 +0900,
Bernard Kenik 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.
This topic is locked and can not be replied to.