liveBook · Manning

Arity

As explained in The Well-Grounded Rubyist, Fourth Edition

Arity is the number of arguments that a callable Ruby object or method accepts according to its argument rules.

Arity is the number of arguments a callable accepts according to its argument rules. Methods enforce a strict argument count, while ordinary Proc objects are more permissive. section 14.1.6 section 16.3.5

A permissive Proc

A one-parameter Proc can be called without arguments, binding its parameter to nil, or with several arguments, using the first and discarding the rest.

Lambda procs restore stricter arity behavior. Ruby’s curry method uses arity to accept arguments incrementally, returning callable objects until enough arguments are available. section 14.1.6 section 16.3.5

Currying a three-argument function

A curried addition function can receive all arguments at once, one at a time, or in groups: fun.(1, 2, 3), fun.(1).(2).(3), and fun.(1, 2)[3] all produce 6.

Arity supports reusable functions: abstracting one argument from repeated operations creates specialized functions, such as find_multiples_of_3 and find_multiples_of_5.

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Explains strict and permissive argument rules, then applies arity to currying, partial application, splat arguments, and method conversion.
Examples
Uses permissive Proc calls, curried addition, staged calls, and reusable functions for selecting array multiples.
What it assumes
Assumes familiarity with Ruby methods, arguments, blocks, Proc objects, lambdas, and callable syntax.
What you can do after
Apply arity by choosing Proc or lambda behavior and by building reusable functions with curry.
How it connects
Connects arity with methods, Proc objects, lambdas, argument lists, partial application, currying, and higher-order functions.

Also explained in

Compare how 5 books explain arity →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Rubyist, Fourth Edition. The section links point to the parts of the book it draws on.