liveBook · Manning

Default value

As explained in The Well-Grounded Rubyist, Fourth Edition

A default argument is a method parameter with a value assigned when the caller does not supply that argument.

Suppose a method usually uses 1 for its third argument, but callers sometimes need another value. Ruby lets the parameter declare that fallback: `def default_args(a, b, c=1)`. The first two arguments are required; `c` is optional. section 2.4.1

Omit or override the argument

With `def default_args(a, b, c=1); p a, b, c; end`, calling `default_args(3, 2)` prints `3`, `2`, and `1`. Calling `default_args(4, 5, 6)` prints `4`, `5`, and `6`. The default is assigned to the parameter only when the caller omits that argument. section 2.4.1

A default-valued parameter is one kind of optional parameter. A `*args` parameter is optional too, but it collects extra arguments into an array rather than supplying a declared fallback. In either case, argument binding assigns values to local variables usable within the method. section 2.4.1 section 2.4.3

When a signature mixes parameter types, keep their binding rules in mind:

  • Required parameters take priority, including ones to the right of optional parameters. In `def m(a, b=2, *c, d)`, a call with two arguments leaves `b` at `2`, gives `c` an empty array, and binds the final argument to `d`. section 2.4.3
  • Do not put a splat parameter before a default-valued parameter: `def broken_args(x, *y, z=1)` is a syntax error. Keep signatures simple when possible. section 2.4.3

Defaults for keyword arguments

Ruby also accepts defaults for named parameters: `def m(a: 1, b: 2); p a, b; end`. Calling `m` uses both defaults; `m(a: 10)` uses `10` for `a` and `2` for `b`. Unlike positional arguments, callers can supply `b` without supplying `a`. section 9.3.8

Do not confuse a parameter default with a hash’s default value. `Hash.new(0)` supplies `0` when a nonexistent key is looked up; it does not declare an optional method parameter or add that key to the hash. section 9.3.3

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Works through omitted and supplied arguments, mixed-parameter binding, and keyword defaults; stops short of evaluating default expressions.
Examples
Uses method calls with omitted or overriding arguments, a mixed signature, and calls supplying selected keyword arguments.
What it assumes
Assumes familiarity with method definitions and calls; introduces optional-argument syntax before returning to keyword arguments after hashes.
What you can do after
Build it from scratch: define Ruby methods with optional positional or keyword parameters and predict how calls bind their arguments.
How it connects
Connects default-valued parameters to required arguments, splat parameters, keyword arguments, and local variable binding.

Also explained in

Compare how 20 books explain default value →

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.