liveBook · Manning

Method name

As explained in The Well-Grounded Rubyist, Fourth Edition

A method name is the identifier by which a Ruby method is referred to, generally following local-variable naming rules with optional ?, !, or = suffixes.

A method name is a Ruby identifier that names the operation being called. In a method call, the name is required; the receiver may be explicit or may default to self, while arguments and a code block are optional. section 1.1.3 section 6.3.3

Recognize method names by contrasting them with nearby Ruby identifier categories:

  • Local variables begin with a lowercase letter or underscore and use letters, underscores, and digits; method names belong to a separate identifier category. section 1.1.3
  • A method call can combine a method name with an explicit receiver, an argument list, a code block, or none of the optional parts. section 6.3.3
  • Keep the argument list distinct from the code block: a block is part of the call syntax, not another argument. section 6.3.3

Method names inside calls

In `string.scan(/[^,]+/)`, `scan` is the method name, `string` is the receiver, and the regular expression is an argument. In `string.scan(/[^,]+/) {|word| puts word }`, the same method name appears with both an argument and a code block. section 6.3.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 compact method-call forms to see how the method name remains required while arguments and blocks vary independently. section 6.3.3

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Defines method names and their place in calls, but stops short of detailed naming conventions or method-definition mechanics.
Examples
Demonstrates scan calls with a receiver, regular-expression argument, optional empty argument list, and code block.
What you can do after
Evaluate method-call syntax by identifying the method name, receiver, arguments, and optional code block.
How it connects
Connects method names with identifiers, receivers, arguments, code blocks, local variables, and self.

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.