Methods
Class methods are defined with the
def statement. The def
statement adds a method to the innermost class or module definition
surrounding the def statement. A
def statement outside a class or module definition
(at the top level) adds a method to the Object
class itself, thus defining a method that can be referenced anywhere
in the program.
When a method is called, Ruby searches for it in a number of places in the following order:
Among the methods defined in that object (i.e., singleton methods).
Among the methods defined by that object’s class.
Among the methods of the modules included by that class.
Among the methods of the superclass.
Among the methods of the modules included by that superclass.
Repeats Steps 4 and 5 until the top-level object is reached.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access