A method is a named block of parameterized code associated with one or more objects. A method invocation specifies the method name, the object on which it is to be invoked (sometimes called the receiver), and zero or more argument values that are assigned to the named method parameters. The value of the last expression evaluated in the method becomes the value of the method invocation expression.
Many languages distinguish between functions, which have no
associated object, and methods, which are invoked on a receiver object.
Because Ruby is a purely object-oriented language, all methods are true
methods and are associated with at least one object. We have not covered
class definitions in Ruby yet, so the example methods defined in this
chapter look like global functions with no associated object. In fact,
Ruby implicitly defines and invokes them as private methods of the
Object
class.
Methods are a fundamental part of Ruby’s syntax, but they are not
values that Ruby programs can operate on. That is, Ruby’s methods are not
objects in the way that strings, numbers, and arrays are. It is possible,
however, to obtain a Method
object that
represents a given method, and we can invoke methods indirectly through
Method
objects.
Methods are not Ruby’s only form of parameterized executable code. Blocks, which we introduced in Blocks, are executable chunks of code and may have parameters. Unlike methods, blocks do not have names, and they can only be invoked indirectly through an iterator method.
Blocks, like methods, are not objects that Ruby can manipulate. But
it’s possible to create an object that represents a block, and this is
actually done with some frequency in Ruby programs. A Proc
object represents a block. Like a Method
object, we can execute the code of a
block through the Proc
that represents
it. There are two varieties of Proc
objects, called procs and
lambdas, which have slightly different behavior. Both
procs and lambdas are functions rather than methods invoked on an object.
An important feature of procs and lambdas is that they are
closures: they retain access to the local variables
that were in scope when they were defined, even when the proc or lambda is
invoked from a different scope.
Methods have a rich and fairly complex syntax in Ruby, and the first four sections of this chapter are dedicated to them. We begin by explaining how to define simple methods, and then follow this introductory section with three more advanced sections covering methods names, method parentheses, and method parameters. Note that method invocation is a kind of expression, covered earlier in Method Invocations. Further details on method invocation are provided throughout the first four sections of this chapter.
After covering methods, we turn our attention to procs and lambdas,
explaining how to create and invoke them, and also detailing the somewhat
subtle differences between them. A separate section covers the use of
procs and lambdas as closures. This is followed by a section on the
Method
object, which actually behaves
much like a lambda. The chapter ends
with an advanced exploration of functional programming in Ruby.
3.137.178.9