liveBook · Manning

Function argument

As explained in The Well-Grounded Rubyist, Fourth Edition

A Ruby argument is a value supplied to a method call and is distinct from a supplied block.

A Ruby argument is a value supplied in a method call. In `obj.c2f(100)`, `100` is the argument; in `def obj.c2f(c)`, `c` is the formal parameter that receives it. Methods return a value, so the conversion can itself be passed to another call: `puts obj.c2f(100)` prints `212.0`. section 2.1.3

Arguments are objects passed to a receiver’s method. In `"100".to_i(9)`, the string receives `to_i`, while `9` tells the method which base to use; the result is `81`. Parentheses around arguments are often optional, but they help avoid ambiguity when calls become more complex. section 1.1.4

The parameter list determines how many arguments a method accepts:

  • A plain parameter requires one argument. Calling a two-parameter method with three raises `ArgumentError` for the wrong number of arguments. section 2.4.1
  • A starred parameter, such as `*c`, collects remaining arguments into an array. With no remaining arguments, that array is empty. section 2.4.1
  • A default-valued parameter, such as `c=1`, uses its default when the caller omits that argument; a supplied value overrides it. section 2.4.1

A call can expose a shared object

Given `def change_string(str); str.replace("New string content!"); end`, calling `change_string(s)` changes the string also referenced by `s`. The parameter receives a reference to the argument object, not an isolated copy. Call `change_string(s.dup)` if this method should change a duplicate instead. section 2.5.3

The same receiver–argument distinction applies to arrays: in `a.concat([4,5,6])`, `a` is the receiver and the other array is the argument. `concat` changes its receiver; `a + [4,5,6]` instead produces a new array without changing `a`. section 9.2.3

A proc can also receive arguments, but its count is less strict than a method’s. For a one-parameter `Proc.new`, a call with no arguments sets the parameter to `nil`; a call with several uses the first and discards the rest. The block used to create the proc is distinct from the values later passed to `pr.call`. section 14.1.6

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Defines arguments against formal parameters, works through required and optional counts, and explains object references; leaves lambda argument rules undeveloped.
Examples
Illustrates arguments with Celsius conversion, wrong-count errors, rest arrays, default values, string mutation, and proc calls.
What it assumes
Assumes basic Ruby object and method-call familiarity while defining the argument–parameter distinction explicitly.
What you can do after
Build it from scratch by defining method parameters, supplying matching arguments, and choosing rest or default parameters when calls vary.
How it connects
Connects arguments to method calls, formal parameters, receivers, object references, mutation, and proc arity.

Also explained in

Compare how 5 books explain function argument →

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.