How to distinguish blocks of certain arity in 1.8

B

bp50fathoms

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.
 
B

bp50fathoms

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

The Higgs bozo

unknown said:
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.
 
B

Bernard Kenik

The said:
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
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.
=> 0


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

Nobuyoshi Nakada

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top