Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: ca97e182e1
Fetching contributors…

Cannot retrieve contributors at this time

65 lines (44 sloc) 3.003 kb

Proc#arity in Ruby 1.8

The docs for Proc#arity state:

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. {}.arity          #=>  0 {||}.arity        #=>  0 {|a|}.arity       #=>  1 {|a,b|}.arity     #=>  2 {|a,b,c|}.arity   #=>  3 {|*a|}.arity      #=> -1 {|a,*b|}.arity    #=> -2

Let's try it:

raganwald:2008-11-07 raganwald$ ruby -v
ruby 1.8.6 (2008-03-03 patchlevel 114) [universal-darwin9.0]
raganwald:2008-11-07 raganwald$ irb
>> {}.arity
=> -1
>> {||}.arity 
=> 0
>> {|*a|}.arity
=> -1

I guess I need to find another way of detecting a proc with no argument declarations. At least until known issue 574 is resolved.


I thought I'd have a look at method arity as well:

class Foo

  def no_args


Foo.module_eval { define_method :no_args_2 do; end }

  => 0
  => -1

It seems that if you explicitly define a method taking no parameters, you get the correct arity. However, if you use define_method and a block, you get the bug again.

My recent work:

JavaScript AllongéCoffeeScript RistrettoKestrels, Quirky Birds, and Hopeless Egocentricity

(Spot a bug or a spelling mistake? This is a Github repo, fork it and send me a pull request!)

Reg Braithwaite | @raganwald

Jump to Line
Something went wrong with that request. Please try again.