Posted over 5 years ago. Visible to the public. Repeats.

How Ruby method lookup works

When you call a method on an object, Ruby looks for the implementation of that method. It looks in the following places and uses the first implementation it finds:

  1. Methods from the object's singleton class (an unnamed class that only exists for that object)
  2. Methods from prepended modules (Ruby 2.0+ feature)
  3. Methods from the object's class
  4. Methods from included modules
  5. Methods from the class hierarchy (superclass and its ancestors)


Let's say we have the following class hierarchy:

class Superclass def action puts "Superclass" end end module IncludedModule def action puts "Included module" super end end module PrependedModule def action puts "Prepended module" super end end module SingletonModule def action puts "Singleton class" super end end class Klass < Superclass include IncludedModule prepend PrependedModule def action puts "Klass" super end end

… and we call a method like this:

instance = instance.extend(SingletonModule) instance.action

Then we get an output as expected:

Singleton class Prepended module Klass Included module Superclass

Does your version of Ruby on Rails still receive security updates?
Rails LTS provides security patches for old versions of Ruby on Rails (3.2 and 2.3).

Owner of this card:

Henning Koch
Last edit:
4 months ago
by Daniel Straßner
look-up, implementation, override, overwrite, extend, include, prepend, execution, order
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Henning Koch to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more