liveBook · Manning

Argument list

As explained in The Well-Grounded Rubyist, Fourth Edition

An argument list is the ordered collection of values supplied to a method call and interpreted according to the method's parameters.

An argument list is the ordered collection of values supplied to a method call. Ruby matches those arguments to the method's parameters. Parentheses are optional in many calls, but explicit parentheses improve clarity when calls become complex. section 2.1.3

Supplying one argument

A Celsius-to-Fahrenheit method defines one parameter, c, and receives 100 through its argument list: obj.c2f(100) returns 212.0. The same call can be written as obj.c2f 100, although parentheses are often preferable. section 2.1.3

An argument list is separate from a code block. A call may have arguments, a block, both, or neither; a block is syntax attached to the call, not another argument. section 6.3.3 section 14.1.2

loop { puts "Hi" } # code block but no arguments
loop() { puts "Hi" } # code block, explicit empty argument list
string.scan(/[^,]+/) # one argument, no code block
string.scan(/[^,]+/) {|word| puts word } # one argument and a code block

Compare calls with no arguments, an explicit empty argument list, one argument, and one argument plus a code block. section 6.3.3

Keep these distinctions in mind:

  • The argument list itself is not an object; Ruby has no ArgumentList class. The individual arguments are objects. section 14.1.2
  • An empty argument list may be written as (), though methods that take no arguments commonly omit the parentheses. section 2.1.3
  • Methods can accept required, optional, and default-valued arguments. section 2.6

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Explains syntax, optional parentheses, object status, and independence from blocks, but stops short of exhaustive argument forms.
Examples
Uses a Celsius-to-Fahrenheit call and contrasts method calls with blocks, empty lists, and regular expressions.
What it assumes
Assumes basic familiarity with Ruby methods and calls while establishing the technical distinction between parameters and arguments.
What you can do after
Evaluate Ruby calls by distinguishing supplied arguments from parameters, blocks, and block-converted procs.
How it connects
Connects argument lists to method calls, parameters, code blocks, procs, and message sending.

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.