Method Names
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 ...
Get The Ruby Programming Language now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.