Ruby Logo

Method-lookup

Learn about method-lookup in Ruby programming.

Home Ruby Method-lookup

Ruby Method Lookup Chain Mastery

Master Ruby's method lookup mechanism, eigenclasses, singleton methods, and the ancestors chain. Understand how Ruby finds and executes methods in its sophisticated object model.

How Method Lookup Works

Method Lookup: When you call a method on an object, Ruby searches through a specific chain of classes and modules to find the method definition. Understanding this chain is crucial for debugging and metaprogramming.

The Ancestors Chain

# Every class has an ancestors chain
String.ancestors
# => [String, Comparable, Object, Kernel, BasicObject]

Array.ancestors
# => [Array, Enumerable, Object, Kernel, BasicObject]

# Method lookup follows this chain
"hello".upcase
# 1. Look in String class ✓ Found!

"hello".nil?
# 1. Look in String class ✗
# 2. Look in Comparable ✗
# 3. Look in Object ✗
# 4. Look in Kernel ✓ Found!

# Custom class with modules
module
Greetings
def
say_hello
"Hello from module!"
end
end

class
Person
include
Greetings
end

Person.ancestors
# => [Person, Greetings, Object, Kernel, BasicObject]

Method Lookup Order

# Method lookup order demonstration
module
A
def
test
;
"From A"
;
end
end

module
B
def
test
;
"From B"
;
end
end

class
TestClass
include
A
include
B
# B is included after A

def
test
"From TestClass"
end
end

TestClass.ancestors
# => [TestClass, B, A, Object, Kernel, BasicObject]

obj = TestClass.new
obj.test
# => "From TestClass" (class method wins)

# Remove class method to see module method
TestClass.remove_method(:test)
obj.test
# => "From B" (B comes before A in ancestors)

Singleton Methods & Eigenclasses

Eigenclasses: Every object has a hidden singleton class (eigenclass) that sits between the object and its class in the method lookup chain. This is where singleton methods are stored.

Singleton Methods

# Adding methods to specific objects
str = "hello"

# Method 1: def object.method_name
def
str.shout
self.upcase + "!!!"
end

# Method 2: define_singleton_method
str.define_singleton_method(:whisper) do
self.downcase + "..."
end

# Method 3: class_eval on singleton_class
str.singleton_class.class_eval do
def
reverse_shout
self.reverse.upcase
end
end

# Use the singleton methods
str.shout
# => "HELLO!!!"
str.whisper
# => "hello..."
str.reverse_shout
# => "OLLEH"

# Other strings don't have these methods
other_str = "world"
other_str.respond_to?(:shout)
# => false

Eigenclass Exploration

# Exploring eigenclasses (singleton classes)
obj = "hello"
eigenclass = obj.singleton_class

# Eigenclass is unique to this object
eigenclass.class
# => Class
eigenclass.superclass
# => String (points to object's class)

# Different objects have different eigenclasses
obj1 = "hello"
obj2 = "world"
obj1.singleton_class == obj2.singleton_class
# => false

# Eigenclass in method lookup
def
obj1.special_method
"I'm special!"
end

# Method lookup: eigenclass -> String -> Comparable -> Object -> Kernel -> BasicObject
obj1.special_method
# => "I'm special!" (found in eigenclass)

Class Methods & Class Eigenclasses

Class Methods: Since classes are objects, they also have eigenclasses! Class methods are actually singleton methods defined on the class object's eigenclass.

Class Method Definition

# Multiple ways to define class methods
class
User
# Method 1: def self.method_name
def
self
.
create
(name)
new(name)
end

# Method 2: class << self
class
<<
self
def
find_by_name
(name)
"Finding user: #{name}"
end

def
count
"Total users: 42"
end
end

def
initialize
(name)
@name = name
end
end

# Method 3: define_singleton_method on class
User.define_singleton_method(:all) do
"All users"
end

# All these are stored in the class's eigenclass
User.singleton_class.instance_methods(false)
# => [:create, :find_by_name, :count, :all]

Class Eigenclass Hierarchy

# Class eigenclasses form their own hierarchy
class
Animal
def
self
.
species_count
"Many species"
end
end

class
Dog
<
Animal
def
self
.
breed_count
"340+ breeds"
end
end

# Dog inherits Animal's class methods
Dog.species_count
# => "Many species"
Dog.breed_count
# => "340+ breeds"

# Eigenclass hierarchy
Dog.singleton_class.superclass
# => # (Animal's eigenclass)
Animal.singleton_class.superclass
# => #

# Method lookup for class methods
Dog.singleton_class.ancestors
# Shows the eigenclass hierarchy

Method Lookup Visualization

Lookup Path: Ruby follows a specific path when looking for methods: eigenclass → class → included modules → superclass → superclass modules → ... → BasicObject.

Complete Lookup Example

# Complex method lookup demonstration
module
Trackable
def
track
"Tracking from module"
end
end

module
Loggable
def
log
"Logging from module"
end

def
track
"Tracking from Loggable"
end
end

class
BaseModel
def
save
"Saving from BaseModel"
end
end

class

Video Tutorial

Watch and learn method-lookup

Pro Tip: After reading through the content above, watch this video to reinforce your understanding and see the concepts in action!