By convention, method names begin with a lowercase letter.
(Method names can begin with a capital letter, but that makes them look
like constants.) When a method name is longer than one word, the usual
convention is to separate the words with underscores like_this
rather than using mixed case
likeThis
.
Method names may (but are not required to) end with an equals sign, a question mark, or an exclamation point. An equals sign suffix signifies that the method is a setter that can be invoked using assignment syntax. Setter methods are described in Assigning to Attributes and Array Elements and additional examples are provided in Accessors and Attributes. The question mark and exclamation point suffixes have no special meaning to the Ruby interpreter, but they are allowed because they enable two extraordinarily useful naming conventions.
The first convention is that any method whose name ends with a
question mark returns a value that answers the question posed by the
method invocation. The empty?
method
of an array, for example, returns true
if the array has no elements. Methods
like these are called predicates. Predicates typically return one of the Boolean values
true
or false
, but this is not required, as any value
other than false
or nil
works like true
when a Boolean value is required. (The
Numeric
method nonzero?
, for example, returns nil
if the number it is invoked on is zero,
and just returns the number otherwise.)
The second convention is that any method whose name ends with an
exclamation mark should be used with caution. The Array object, for
example, has a sort
method that makes
a copy of the array, and then sorts that copy. It also has a sort!
method that sorts the array in place.
The exclamation mark indicates that you need to be more careful when
using that version of the method.
Often, methods that end with an exclamation mark are mutators, which alter the
internal state of an object. But this is not always the case; there are
many mutators that do not end with an exclamation mark, and a number of
nonmutators that do. Mutating methods (such as Array.fill
) that do not have a nonmutating
variant do not typically have an exclamation point.
Consider the global function exit
: it makes the Ruby program stop running
in a controlled way. There is also a variant named exit!
that aborts the program immediately
without running any END
blocks or
shutdown hooks registered with at_exit
. exit!
isn’t a mutator; it’s the “dangerous”
variant of the exit
method and is
flagged with !
to remind a programmer
using it to be careful.
Many of Ruby’s operators, such as +
, *
, and
even the array index operator []
,
are implemented with methods that you can define in your own classes.
You define an operator by defining a method with the same “name” as
the operator. (The only exceptions are the unary plus and minus
operators, which use method names +@
and -@
.) Ruby allows you to do this even
though the method name is all punctuation. You might end up with a
method definition like this:
def +(other) # Define binary plus operator: x+y is x.+(y) self.concatenate(other) end
Table 4-2 in Chapter 4 specifies which of Ruby’s operators are defined as methods. These operators are the only punctuation-based method names that you can use: you can’t invent new operators or define methods whose names consist of other sequences of punctuation characters. There are additional examples of defining method-based operators in Defining Operators.
Methods that define a unary operator are passed no arguments.
Methods that define binary operators are passed one argument and
should operate on self
and the
argument. The array access operators []
and []=
are special because they can be invoked
with any number of arguments. For []=
, the last argument is always the value
being assigned.
It is not uncommon for methods in Ruby to have more than one name. The
language has a keyword alias
that
serves to define a new name for an existing method. Use
it like this:
alias aka also_known_as # alias new_name existing_name
After executing this statement, the identifier aka
will refer to the same method thats
also_known_as
does.
Method aliasing is one of the things that makes Ruby an
expressive and natural language. When there are multiple names for a
method, you can choose the one that seems most natural in your code.
The Range class, for example, defines a method for testing whether a
value falls within the range. You can call this method with the name
include?
or with the name member?
. If you are treating a range as a
kind of set, the name member?
may
be the most natural choice.
A more practical reason for aliasing methods is to insert new functionality into a method. The following is a common idiom for augmenting existing methods:
def hello # A nice simple method puts "Hello World" # Suppose we want to augment it... end alias original_hello hello # Give the method a backup name def hello # Now we define a new method with the old name puts "Your attention please" # That does some stuff original_hello # Then calls the original method puts "This has been a test" # Then does some more stuff end
In this code, we’re working on global methods. It is more common
to use alias
with the instance
methods of a class. (We’ll learn about this in Chapter 7.) In this situation, alias
must be used within the class
whose method is to be renamed. Classes
in Ruby can be “reopened” (again, this is discussed in Chapter 7)—which means that your code can take an existing
class, ‘open’ it with a class
statement, and then use alias
as
shown in the example to augment or alter the existing methods of that
class. This is called “alias chaining” and is covered in detail in
Alias Chaining.
3.17.154.139