liveBook · Manning

Method call

As explained in The Well-Grounded Rubyist, Fourth Edition

A method call is a Ruby expression that invokes a method on an object, optionally supplying arguments and a block.

A call and its result

In `x = "100".to_i`, the string is the receiver. The dot sends it the message `to_i`; the resulting integer, `100`, is assigned to `x`. “Calling a method” describes the usual result of sending a message, but a receiver is not guaranteed to have a matching method. section 1.1.4

A method call has a method name and, optionally, an argument list and a code block. An explicit receiver needs a dot; without one, the receiver defaults to `self`. Every call returns a value. Calls are how objects are asked to provide information or perform actions. section 6.3.3 section 2.1.3 section 2.6

Keep these parts distinct:

  • Arguments are values supplied at the call site; parameters are variables named in the method definition. Parameters may be required, collect extra arguments, or have defaults. Supplying the wrong number of required arguments raises `ArgumentError`. section 2.1.3 section 2.4.1
  • A block belongs to the call but is not an argument. A method can yield to it only when one is supplied; some methods also work without one. section 6.3.3
  • Parentheses around arguments are often optional, but they help clarify calls strung together. For a no-argument call, omitting them is common. section 2.1.3
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 `loop` with and without an explicit empty argument list, then `scan` with an argument alone and with both an argument and a block. section 6.3.3

A bare word can also be a method call, as with `puts`. Parentheses identify `x()` as a call; without them, Ruby may need to resolve the name as either a local variable or a method. This distinction matters when reading an undefined-name error. section 2.5.4

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Explains receivers, arguments, returns, blocks, and bareword resolution, while deferring fuller treatment of block parameters.
Examples
Contrasts string conversion, ticket queries, argument-count errors, and calls with or without blocks.
What it assumes
Starts with string literals and variables as receivers, then develops argument lists and blocks as separate parts of calls.
What you can do after
Diagnose/debug with it by distinguishing arguments from blocks and bareword calls from local variables.
How it connects
Connects method calls to message sending, receivers, local variables, parameters, return values, and code blocks.

Also explained in

Compare how 2 books explain method call →

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.