Arity?

From the Programming Ruby - The Pragmatic Programmer’s Guide:

–class Proc —
arity prc.arity -> anInteger

Returns the number of arguments required by the block. If the block
takes no arguments, returns -1. If it takes one argument, returns -2.
Otherwise, returns a positive argument count unless the last argument
is prefixed with *, in which case the argument count is negated. The
number of required arguments is anInteger for positive values, and
( anInteger +1).abs otherwise.

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

–class Method –

arity meth.arity -> aFixnum

Returns an indication of the number of arguments accepted by a method.
Returns a nonnegative integer for methods that take a fixed number of
arguments. For Ruby methods that take a variable number of arguments,
returns -n-1, where n is the number of required arguments. For methods
written in C, returns -1 if the call takes a variable number of arguments.

···

I can make neither head nor tail of this as the values that are actually returned
are not those in the example, and the text doesn’t appear to make any sense at
all to me. Can someone explain what arity means and how it’s determined?


J. Lambert

From the Programming Ruby - The Pragmatic Programmer’s Guide:

–class Proc —
arity prc.arity → anInteger

Returns the number of arguments required by the block. If the block
takes no arguments, returns -1. If it takes one argument, returns -2.
Otherwise, returns a positive argument count unless the last argument
is prefixed with *, in which case the argument count is negated. The
number of required arguments is anInteger for positive values, and
( anInteger +1).abs otherwise.

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

mh I het different results:

Proc.new do end.arity
=> -1 # no args
Proc.new do|| end.arity
=> 0 #0 args
Proc.new do|one| end.arity
=> 1 #one arg
Proc.new do|one,two| end.arity
=> 2 #two arg
Proc.new do|*maybenoargs| end.arity
=> -1 #no args (as minimum)

about the -2… dunno :frowning:

···

il Mon, 15 Dec 2003 09:52:50 +0900, “Jon A. Lambert” jlsysinc@alltel.net ha scritto::

#arity seems to have been changed since that was written.

Seems to me it does this:

if #arity is negative (n < 0)

the minimum number of required parameters is (n + 1).abs

proc {|*a|}.arity #=> -1, can be called with 0 or more args
proc {|a,*b|}.arity #=> -2, can be called with 1 or more args

In the above, calling with 0 or 1 args respectively means a and b
respectively are [].

If #arity is positive or 0, the number of formal parameters is
exactly n.

proc {|a|}.arity #=> 1
proc {||}.arity #=> 0
proc {|a,b|}.arity #=> 2

Trying to pass a number of arguments other than n will be an error except in
the case of 1, where the arguments will be collected into an array and
placed in
the one parameter. However, methods of one parameter can only be called
with one argument (making them different in this regard from methods).

proc {}.arity #=> -1

This is because you can pass in any number of arguments and they’ll be
ignored.
To specify 0 params and only 0 params, you need ||.

I think that’s every possibility. If I’ve missed something let me know.
I’m working
with Ruby version 1.8.0 ms-win32 build.

  • Dan

I experienced this same confusion.

What I figured out (which I think is correct) is that the documentation
you quoted from “Programming Ruby” is correct for Ruby 1.6

The simpler results (0 for no-args, n for n args, -n for variable length,
minimum of n args) are for Ruby 1.8.

Here’s two tests on different ruby versions. Note that the only
difference is methods that take 1 argument:

lewisd@wedge:~/projects/rcoaster$ ruby -v
ruby 1.6.8 (2002-12-24) [i386-linux]
lewisd@wedge:~/projects/rcoaster$ ruby ./test/testArity.rb

 = 0

a> = -1
a,b| = 2
a,b,c| = 3
*a| = -1
a,*b| = -2
a,b,*c| = -3
lewisd@wedge:~/projects/rcoaster$

lewisd@derlewi:~/projects/rcoaster$ ruby -v
ruby 1.8.1 (2003-11-11) [i386-linux]
lewisd@derlewi:~/projects/rcoaster$ ruby ./test/testArity.rb

 = 0

a> = 1
a,b| = 2
a,b,c| = 3
*a| = -1
a,*b| = -2
a,b,*c| = -3
lewisd@derlewi:~/projects/rcoaster$

lewisd@wedge:~/projects/rcoaster$ cat ./test/testArity.rb
puts "|| = " + Proc.new {||}.arity.to_s
puts "|a| = " + Proc.new {|a|}.arity.to_s
puts "|a,b| = " + Proc.new {|a,b|}.arity.to_s
puts "|a,b,c| = " + Proc.new {|a,b,c|}.arity.to_s
puts "|*a| = " + Proc.new {|*a|}.arity.to_s
puts "|a,*b| = " + Proc.new {|a,*b|}.arity.to_s
puts "|a,b,*c| = " + Proc.new {|a,b,*c|}.arity.to_s
lewisd@wedge:~/projects/rcoaster$

···

On Mon, 15 Dec 2003, Jon A. Lambert wrote:

From the Programming Ruby - The Pragmatic Programmer’s Guide:

–class Proc —
arity prc.arity → anInteger

Returns the number of arguments required by the block. If the block
takes no arguments, returns -1. If it takes one argument, returns -2.
Otherwise, returns a positive argument count unless the last argument
is prefixed with *, in which case the argument count is negated. The
number of required arguments is anInteger for positive values, and
( anInteger +1).abs otherwise.

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

–class Method –

arity meth.arity → aFixnum

Returns an indication of the number of arguments accepted by a method.
Returns a nonnegative integer for methods that take a fixed number of
arguments. For Ruby methods that take a variable number of arguments,
returns -n-1, where n is the number of required arguments. For methods
written in C, returns -1 if the call takes a variable number of arguments.


I can make neither head nor tail of this as the values that are actually returned
are not those in the example, and the text doesn’t appear to make any sense at
all to me. Can someone explain what arity means and how it’s determined?


J. Lambert

Derek Lewis

===================================================================
Java Web-Application Developer

  Email    : email@lewisd.com
  Cellular : 604.312.2846
  Website  : http://www.lewisd.com

“If you’ve got a 5000-line JSP page that has “all in one” support
for three input forms and four follow-up screens, all controlled
by “if” statements in scriptlets, well … please don’t show it
to me :-). Its almost dinner time, and I don’t want to lose my
appetite :-).”
- Craig R. McClanahan