liveBook · Manning

Input

As explained in The Well-Grounded Rubyist, Fourth Edition

A parameter is a named input in a method definition or block that receives a corresponding argument when the callable is invoked.

A parameter is a variable named in a method definition to receive a value when the method is called. In `def obj.c2f(c)`, `c` is the formal parameter; in `obj.c2f(100)`, `100` is the argument. The method uses the received value to calculate a result. Ruby permits parentheses to be omitted in many definitions and calls, but keeping them is often clearer, especially when calls are combined. section 2.1.3

A method signature determines how arguments are assigned:

  • A plain parameter is required: each one needs a corresponding argument. Supplying the wrong number raises `ArgumentError`. section 2.4.1
  • A default-valued parameter, such as `c=1`, receives its default when no argument is supplied for it; an argument overrides the default. section 2.4.1
  • A starred parameter, such as `*c`, collects remaining arguments in an array, which is empty if none remain. section 2.4.1

Required parameters take priority

With `def mixed_args(a,b,*c,d)`, calling `mixed_args(1,2,3,4,5)` assigns `1` and `2` to `a` and `b`, `5` to `d`, and `[3,4]` to `c`. Required parameters on either side of `*c` receive values before the starred parameter collects what remains. Default-valued and starred parameters belong between required parameters; a starred parameter cannot precede a default-valued one. section 2.4.2 section 2.4.3

Argument binding gives method parameters local variables usable during the method call. Blocks also have parameters: `5.times { |i| ... }` binds `i` to each yielded value, from `0` through `4`. A proc is less strict about argument count than a method: a missing argument binds its parameter to `nil`, while extra arguments can be discarded. section 2.4.3 section 6.3.5 section 14.1.6

Keep signatures reasonably simple when combining required, default-valued, and starred parameters. Keyword arguments are another form of argument handling, treated separately from these positional examples. section 2.4.3 section 2.4

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Works through required, default-valued, and starred binding and ordering; defers full treatment of keyword arguments.
Examples
Uses a Celsius converter, mixed method signatures, and iteration with `times`.
What it assumes
Assumes basic Ruby method calls while defining the parameter–argument distinction and introducing binding rules.
What you can do after
Practice/perform method signature design and predict which values bind to positional, default-valued, and starred parameters.
How it connects
Connects parameters to arguments, local variables, arrays, blocks, and procs.

Also explained in

Compare how 14 books explain input →

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.