liveBook · Manning

Same-named method

As explained in The Well-Grounded Rubyist, Fourth Edition

A same-named method is one of multiple methods sharing a name across the classes and modules in an object's method lookup path.

An object can encounter multiple methods with the same name because its method-lookup path crosses several classes and modules. It executes only the first matching definition. section 4.2.2

Class method versus mixed-in method

If BankAccount includes InterestBearing and both define calculate_interest, an account object finds BankAccount first and runs that implementation. The class method overrides the mixed-in module method. section 4.2.2

When several modules define the same method:

  • Search included modules in reverse inclusion order, so the most recently included module takes precedence. section 4.2.2
  • Treat repeated same-named methods cautiously; intentional overriding can be useful, but accidental collisions make code harder to read. section 4.2.2

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Works through class and module precedence, including reverse inclusion order, but stops short of broader dispatch internals.
Examples
Uses BankAccount and InterestBearing, then contrasts modules M and N defining report.
What it assumes
Assumes familiarity with objects, classes, modules, and including modules into classes.
What you can do after
Diagnose which same-named method an object will execute from its class and module inclusion order.
How it connects
Connects method overriding with classes, modules, mixins, and method lookup paths.

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.